switch privileges inside batch

Discussion in 'Scripting' started by Thomas Dubreuil, Apr 14, 2019.

  1. Thomas Dubreuil

    Thomas Dubreuil MDL Senior Member

    Aug 29, 2017
    363
    620
    10
    I explain my case first, the solution may be simple...
    in short I have a script running in admin and killing/restarting explorer at the end...
    each time it restarts explorer CreateExplorerShellUnelevatedTask is created.

    I already know how to "elevate" with NSudo or NirCmd with goto like
    NSudoC.exe -U:T -P:E -Wait -UseCurrentConsole "%~dpnx0"& goto :there

    Same way I can launch explorer as "normal user" with Nsudo, but is there another (simpler) way to "unelevate" the script?
    Because, then the problem with NSudo is when I run the kill command with Nsudo, I can successfully erase NSudo but not its folder in the script's cleaning task" at the end.
    When I try to delete the folder manually it says folder in use...by explorer I guess.

    Only solution so far is to extract/copy NSudo separately to a regular/existing directory like TEMP and not create one.

    I also tried runas /trustlevel:0x20000 "explorer.exe" but that doesn't seem to work.

    Not a big deal I can also add my schTasks /query /TN "CreateExplorerShellUnelevatedTask" >NUL 2>&1 && schTasks /Delete /TN "CreateExplorerShellUnelevatedTask" /f >NUL 2>&1...

    Would be interested to know if someone has good trick to switch privileges.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...