【问题标题】:Run a Powershell script from Batch file with elevated privileges?以提升的权限从批处理文件运行 Powershell 脚本?
【发布时间】:2018-02-18 23:54:33
【问题描述】:

我想从code.bat运行example.ps1

我知道start 命令会运行它,但要求它以管理员身份运行,我该怎么做?

【问题讨论】:

    标签: powershell batch-file


    【解决方案1】:

    在您的批处理文件中:

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

    ...基本上你正在使用 Powershell 来运行 Powershell。第二个实例被提升为管理员,并正在使用这些权限运行您的脚本。


    完整解释:

    Start-Process 可用于运行程序,也有参数-Verb RunAs 将程序提升为管理员运行。

    我们不能从批处理文件中调用Start-Process,因为它是一个 PowerShell 命令。

    但我们可以从批处理文件中运行powershell,然后使用-command 参数运行Start-Process

    我们使用Start-Process(再次)运行powershell,并在脚本提升为管理员时运行您的脚本:-ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs

    【讨论】:

    • 我已经走到这一步了。
    • powershell -noprofile -command "&{ Start-Process powershell -ArgumentList '-NoProfile -File %path%Resources\Found\WindowsSecurity.ps1' -Verb RunAs}"
    • 感谢您的帮助!你能稍微解释一下吗? :D
    • 很高兴我能提供帮助,如果您对我的回答感到满意,您可以Mark it as Accepted
    • 谢谢你的好意。
    猜你喜欢
    • 2018-08-03
    • 2012-10-20
    • 2013-10-20
    • 2013-02-18
    • 1970-01-01
    • 2013-05-31
    • 2021-06-22
    • 1970-01-01
    • 2015-01-03
    相关资源
    最近更新 更多