johny why
johny why

Reputation: 2201

SETX with RUNAS on Windows 10 Fails

This statement fails.

runas /noprofile /env /savecred /user:"Eve" setx path "%path%;D:\Program Files (x86)\metapad36" /M

Works with notepad, by loading the notepad into a variable, and passing that variable to runas.

But with setx, I do not receive an error message. The console displays the help-file for runas.

Appreciate any help.

thx!

Update:

This works:

>set myvar=notepad
>echo "%myvar%"
"notepad"

And then this works:

runas /noprofile /env /savecred /user:Adam "%myvar%"

This seems to work. It expands the value of "%path%", which is fine. Some of the comments assume i need the original % variables in the command i pass to runas, but it's fine if expanded to their actual value:

>set myvar=setx path "%path%;D:\Program Files (x86)\metapad36" /M
>echo "%myvar%"
"setx path "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin;;D:\Program Files (x86)\metapad36" /M"

But then this fails:

>runas /noprofile /env /savecred /user:Adam "%myvar%"

Update2:

If i run this with a non-existent username in the /user parameter, i get asked for their password. But if i run it with a real admin username (in a non-admin command prompt), i do NOT get asked for a password. Instead, an unknown window flashes and disappears, and the system path does NOT get changed.

>runas /noprofile /env /savecred /user:"Eve" "setx path \"%path%;D:\Program Files (x86)\metapad36\" /M"
Attempting to start setx path "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin;;D:\Program Files (x86)\metapad36" /M as user "DESKTOP-AB0HSLD\Eve" ...
Enter the password for Eve:

>runas /noprofile /env /savecred /user:"Adam" "setx path \"%path%;D:\Program Files (x86)\metapad36\" /M"
Attempting to start setx path "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\ProgramData\chocolatey\bin;D:\Program Files (x86)\Microsoft VS Code\bin;;D:\Program Files (x86)\metapad36" /M as user "DESKTOP-AB0HSLD\Adam" ...

i placed this code in a batch file, and executed it. Still getting flashing screen and same "attempting" output shown above.

still quite confused...

Upvotes: 1

Views: 552

Answers (1)

Ross Presser
Ross Presser

Reputation: 6255

  1. The command needs to be one argument -- possibly enclosed by quotes. The additional quotes being used to surround the path argument need to be enclosed. Either save this command in a .cmd batch file, or use backslash to escape the quotes.

    As Wapac has mentioned in comments, the backslash escape is specifically recommended by runas /? help text. But using a batch file is probably preferable because it doesn't seem possible to escape the percent signs.

  2. How can you expect setx to accomplish anything if you runas with /noprofile?

Upvotes: 0

Related Questions