简体   繁体   中英

How to pass quoted arguments to elevated batch script via powershell Start-Process

I have a really hard time passing quotes to .bat files when elevating, binaries seem to work thou... steps to reproduce:

create a small test batch script %TEMP%\\test.bat containing

echo.%* && pause

Fire up powershell and try those:

# both behave like expected, echoing hello and pausing
saps -filepath cmd -argumentlist '/c echo.hello && pause'
saps -filepath "$env:TEMP\test.bat" -argumentlist 'hello'

# both behave like expected, echoing "hello" and pausing
saps -filepath cmd -argumentlist '/c echo."hello" && pause'
saps -filepath "$env:TEMP\test.bat" -argumentlist '"hello"'

# both behave like expected, echoing an elevated hello and pausing
saps -verb runas -filepath cmd -argumentlist '/c echo.hello && pause'
saps -verb runas -filepath "$env:TEMP\test.bat" -argumentlist 'hello'

# cmd still echoes correctly "hell"no and pauses
saps -verb runas -filepath cmd -argumentlist '/c echo."hell"no && pause'

# tl;dr

# now this is where hell breaks loose
saps -verb runas -filepath "$env:TEMP\test.bat" -argumentlist '"hell"no'
# doesnt echo anything, window pops up and vanishes in an instant
# doesnt pause

The "runas" verb doesn't work if you invoke it directly on a batch file. You need to invoke it on the actual executable (ie cmd.exe ) and pass the batch file as an argument.

$params = '/c', "$env:TEMP\test.bat", '"hell"no'
Start-Process -Verb Runas -FilePath $env:ComSpec -ArgumentList $params

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM