【问题标题】:Run script as admin in Powershell [duplicate]在Powershell中以管理员身份运行脚本[重复]
【发布时间】:2018-07-29 12:31:02
【问题描述】:

我编写了一个小型 Power Shell 脚本,它正在将证书和 appxupload 文件安装到系统中,但问题是我希望每当用户运行该脚本时,它都应该以管理员模式运行。

我的脚本是:

$CertLocation =  Get-ChildItem -Recurse | Where-Object{$_.Name -like "*.cer"}
Set-Location $CertLocation.Directory 
certutil.exe -addstore TrustedPeople $CertLocation.Name
$AppPackage =  Get-ChildItem -Recurse | Where-Object{$_.Name -like "*.appxbundle"}
Add-AppxPackage -Path $AppPackage.Name

请帮我查询。

【问题讨论】:

  • 不是一个直接的答案,但你可以看看名为 elevate :code.kliu.org/misc/elevate 的工具过去曾为我工作过几件事。

标签: powershell powershell-3.0


【解决方案1】:

在脚本的开头添加以下行。

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}

Here 是相同的 SO 链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多