1

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...

Cœur
  • 37,241
  • 25
  • 195
  • 267
johny why
  • 2,047
  • 7
  • 27
  • 52
  • 1) If possible, always use batch files, this will save you from the escaping hell. 2) as for "unknown window flashes" - when you have this batch file from 1), you can run "cmd.exe /k mybatchfile.cmd" as a command. The /k switch will make the new shell window to stay there after the batch file is finished, so you will actually see what is going on there and will be able to find out what was wrong. – Wapac Feb 15 '16 at 17:01
  • thx for the suggestion! but i'm not scared of escaping hell, just want to solve it, hopefully with help from ppl who understand better than me. Re the batch file, if no one here actually knows the solution to my issue, then i'll try the batch file to troubleshoot. But hopefully, someone here gets it. – johny why Feb 15 '16 at 17:36
  • @Wapac, i placed this code in a batch file, and executed it. Still getting flashing screen and same "attempting" output shown above. – johny why Feb 17 '16 at 20:48
  • hi, just pinging the universe that this topic is still unsolved. I'm happy to experiment if anyone can offer some suggestions. Already tried putting the command in a batch file, but still no joy. thx! – johny why Feb 29 '16 at 02:43

1 Answers1

0
  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?

Community
  • 1
  • 1
Ross Presser
  • 6,027
  • 1
  • 34
  • 66
  • escaping " is with backslash - see runas help examples: `> runas /env /user:user@domain.microsoft.com "notepad \"my file.txt\""`; also that %path% will cause a problem, so batch file is better to be used there – Wapac Feb 14 '16 at 09:59
  • @Wapac: Carets worked for escaping quotes in previous versions and still work in Windows 10. I do not know if backslash escaping was introduced in Windows 10, or in Windows 8.1 or Windows 8; but definitely up until Windows 7, carets were required. – Ross Presser Feb 14 '16 at 10:07
  • The % can be escaped too if necessary. – Ross Presser Feb 14 '16 at 10:08
  • I have just tested with caret on Windows 10 and it did not work, slashes works well. I was unable to figure out how to escape % so that it works properly. I tried `runas ... "cmd.exe /k set x=%path%"` and this failed (path was expanded in the initial shell), but with escaped version `runas ... "cmd.exe /k set x=^%path^%"` it also failed because in the new shell `echo %x%` gave me "%path%" instead of the path itself. – Wapac Feb 14 '16 at 10:19
  • I don't want to vote you down if I am not sure about it. It just does not work for me on my Windows 10, not sure why, but that does not make you wrong. And the issue I have with escaping % is so weird that I am rather creating a new SO question. – Wapac Feb 14 '16 at 10:32
  • Maybe you're right... a bit more work and I now think carets are escaping sometimes, being eaten sometimes, and just broken other times. :( Oh well – Ross Presser Feb 14 '16 at 10:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103414/discussion-between-ross-presser-and-wapac). – Ross Presser Feb 14 '16 at 10:34
  • plz see update in my OP. Also, this article says %'s can be escaped by doubling them. However, as i mentioned in the update, if the % variables expand, that's fine-- i don't require the actual %variable names in the runas command. http://ss64.com/nt/syntax-esc.html – johny why Feb 14 '16 at 23:20
  • re the flashing screen that passes by too quickly to see any return msgs, is there a way to retrieve such messages from some system log? thx – johny why Feb 29 '16 at 02:45