Run a Powershell script from Batch file with elevated privileges?

假如想象 提交于 2020-01-25 10:33:06

问题


I want to run example.ps1 from code.bat

I realize that the start command would run it but require that it be run as admin, how can I do this?


回答1:


In your batch file:

powershell -Command "&{ Start-Process powershell -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs}"

...basically you're using Powershell to run Powershell. The second instance is elevated to Admin and is running your script with those permissions.


Full explanation:

Start-Process can be used to run a program, and also has the parameter -Verb RunAs which elevates the program to run as Admin.

We can't call Start-Process from a batch file as it's a PowerShell command.

But we can run powershell from a batch file, then using the -command parameter to run Start-Process.

We use Start-Process to run powershell (again) and run your script when it is elevated to Admin: -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs



来源:https://stackoverflow.com/questions/48838760/run-a-powershell-script-from-batch-file-with-elevated-privileges

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!