runas does not allow complex arguments?

2019-07-03 15:14发布

问题:

I have an app that I'm trying to run elevated on windows 7 and windows xp thin clients but I cant seem to get the runas.exe cmd line correct. I know I need the backslash escape character in there so runas interprets the spaces correctly. This works when sending runas a single argument that has been escaped with a backslash. This scenario is all I found as a solution and it works however, I need to send multiple arguments that are all escaped with backslashes because of spaces in the arguements due to file paths, etc. Here are some examples:

These work And as expected will prompt for the administrator password:

runas.exe /user:Administrator "C:\update_andon.exe autosetup /kiosktype:\"Andon Kiosk"\

runas.exe /user:Administrator "C:\update_andon.exe autosetup /directory:\"C:\Program Files\CIMS_Andon\Kiosk"\

When I start adding the rest of the arguments I need such as the line below, I receive the runas help text indicating there was an issue:

runas.exe /user:administrator "C:\update_andon.exe autosetup /kiosktype:\"Andon Kiosk"\ /exename:\"eAndonKiosk.exe"\ /directory:\"C:\Program Files\CIMS_Andon\Kiosk"\ /repository:\"\\domain\sat\shared\repository\andon kiosk"\"

One solution is to run each argument at a time one after the other but I dont see this as a solution as much as it is a workaround. Am I missing something with the way I have the backslashes mixed in with the various arguments? Am I missing more quotes somewhere?

Thanks

回答1:

Canonical solution: write the command to a batch script and run the script via runas.



回答2:

It looks like on your trailing quotes you are not escaping them correctly. The backslash is used to escape the interpreter from looking at the next character. for your trailing quotes you are putting the backslash after the quotes, which is not correct.

example from help

"notepad \"my file.txt\""

Note that the backslash is before the quotes. You can even try it out when typing an answer on stack exchange using their markdown since you have to enter a double \ to get a single backslash to show up

original code

runas.exe /user:administrator "C:\update_andon.exe autosetup /kiosktype:\"Andon Kiosk"\ /exename:\"eAndonKiosk.exe"\ /directory:\"C:\Program Files\CIMS_Andon\Kiosk"\ /repository:\"\\domain\sat\shared\repository\andon kiosk"\"

corrected code

runas.exe /user:administrator "C:\update_andon.exe autosetup /kiosktype:\"Andon Kiosk\" /exename:\"eAndonKiosk.exe\" /directory:\"C:\Program Files\CIMS_Andon\Kiosk\" /repository:\"\\domain\sat\shared\repository\andon kiosk\""

references

  1. Quotes, Escape Characters, Delimiters - Windows CMD - SS64.com
  2. Batch files - Escape Characters