我想运行example.ps1
从code.bat
我意识到该start
命令会运行它,但要求它以管理员身份运行,我该怎么做?
在您的批处理文件中:
powershell -Command "&{ Start-Process powershell -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs}"
...基本上你正在使用 Powershell 来运行 Powershell。第二个实例被提升为 Admin 并使用这些权限运行您的脚本。
完整解释:
Start-Process
可以用来运行一个程序,还有一个参数-Verb RunAs
可以提升程序以管理员身份运行。
我们无法Start-Process
从批处理文件中调用,因为它是 PowerShell 命令。
但是我们可以powershell
从批处理文件运行,然后使用-command
参数运行Start-Process
.
当脚本被提升为管理员时,我们Start-Process
用来powershell
(再次)运行并运行您的脚本:-ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句