如有必要,我使用以下代码重新运行具有管理员权限的脚本。
# Require admin
if(!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}
但是,在某些系统上,当我右键单击脚本并选择run with powershell时,它会提示我输入yes、no、all、cancel... 等内容。我不知道这会发生在什么时候。如果没有上述提示,如何强制执行策略更改?-force
我检查了文档,似乎没有powershell
. -force
不过,cmdlet有一个参数set-executionpolicy
。