1

From cmd I'm using the powershell command to elevate a batch script and pass it arguments. The arguments contain double quotes but I can't figure out why it's not working.

Here's my command:

powershell -command "Start-Process -FilePath 'C:\My Path\update.bat' -ArgumentList '\"My File.zip\"'"

The above command works fine and my batch script receives the argument in double quotes, but when I change the command to elevate using -Verb Runas is where I have the problem (the batch script opens, but immediately closes):

powershell -command "Start-Process -Verb Runas -FilePath 'C:\My Path\update.bat' -ArgumentList '\"My File.zip\"'"
double-beep
  • 5,031
  • 17
  • 33
  • 41

1 Answers1

0

Final solution derived from @Compo's response above. This solution worked for me and handles when the batch script's path contains spaces too.

set "Args=%*"
net file 1>nul 2>&1 || (powershell -Command ^
Start-Process -Verb RunAs -FilePath '%comspec%' -ArgumentList '/c \""\""%~f0\"" %Args:"=\""%\""'
goto :eof)
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Kudos for handling the case where the batch file path has embedded spaces, but this solution still isn't fully robust - see https://stackoverflow.com/a/54701990/45375 – mklement0 Feb 15 '19 at 03:13
  • @mklement0, I agree. I'm working on a better solution but this at least got me past some of the initial issues I was seeing. I will follow up once I'm finished. – Jonathan Y. Feb 15 '19 at 13:05
  • The [linked answer](https://stackoverflow.com/a/54701990/45375) is my attempt to create a fully robust solution; I invite you to try it and tell us if there are cases it doesn't handle correctly - I'm happy to amend it, if possible. – mklement0 Feb 15 '19 at 13:11
  • 1
    @mkelement0, where were you a day ago?? Worked perfect, thanks you. – Jonathan Y. Feb 15 '19 at 14:56