首页 > 解决方案 > 命令提示符 powershell 命令以提升批处理文件传递参数

问题描述

从 cmd 我使用 powershell 命令来提升批处理脚本并传递参数。参数包含双引号,但我不知道为什么它不起作用。

这是我的命令:

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

上面的命令工作正常,我的批处理脚本接收双引号中的参数,但是当我将命令更改为提升使用时,我遇到-Verb Runas了问题(批处理脚本打开,但立即关闭):

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

标签: powershellcmdrunaselevation

解决方案


最终解决方案来自@Compo 上面的回复。当批处理脚本的路径也包含空格时,此解决方案对我有用并且可以处理。

set "Args=%*"
net file 1>nul 2>&1 || (powershell -Command ^
Start-Process -Verb RunAs -FilePath '%comspec%' -ArgumentList '/c \""\""%~f0\"" %Args:"=\""%\""'
goto :eof)

推荐阅读