如何通过powershell Start-Process将引用的参数传递给提升的批处理脚本

时间:2017-03-08 11:00:20

标签: windows powershell parameter-passing double-quotes

我很难在提升时将引号传递给.bat文件,二进制文件似乎可以正常工作......重现步骤:

创建一个包含

的小型测试批处理脚本%TEMP%\test.bat
echo.%* && pause

启动PowerShell并试用它们:

# 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

1 个答案:

答案 0 :(得分:0)

" runas"如果直接在批处理文件上调用动词,则动词不起作用。您需要在实际可执行文件(即cmd.exe)上调用它,并将批处理文件作为参数传递。

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