简体   繁体   English

如何通过Powershell启动过程将带引号的参数传递给提升的批处理脚本

[英]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: 我在提升时很难将引号传递给.bat文件,二进制文件似乎可以正常工作...复制步骤:

create a small test batch script %TEMP%\\test.bat containing 创建一个包含以下内容的小型测试批处理脚本%TEMP%\\test.bat

echo.%* && pause

Fire up powershell and try those: 启动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

The "runas" verb doesn't work if you invoke it directly on a batch file. 如果直接在批处理文件上调用“ runas”动词,则该动词将不起作用。 You need to invoke it on the actual executable (ie cmd.exe ) and pass the batch file as an argument. 您需要在实际的可执行文件(即cmd.exe )上调用它,并将批处理文件作为参数传递。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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