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



我很难将报价传递给.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:TEMPtest.bat" -argumentlist 'hello'
# both behave like expected, echoing "hello" and pausing
saps -filepath cmd -argumentlist '/c echo."hello" && pause'
saps -filepath "$env:TEMPtest.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:TEMPtest.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:TEMPtest.bat" -argumentlist '"hell"no'
# doesnt echo anything, window pops up and vanishes in an instant
# doesnt pause

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

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

最新更新