我有一个脚本试图运行这些...
Set-ExecutionPolicy -scope CurrentUser RemoteSigned -Force -ea silent
Set-ExecutionPolicy RemoteSigned -Force -ea silent
但是我收到此错误:
Set-ExecutionPolicy : Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a
more specific scope. Due to the override, your shell will retain its current effective execution policy of Bypass. Type "Get-ExecutionPolicy
-List" to view your execution policy settings.
所以我试了这个:
if ($(Get-ExecutionPolicy) -ne "RemoteSigned") {
Set-ExecutionPolicy -scope CurrentUser RemoteSigned -Force -ea silent
Set-ExecutionPolicy RemoteSigned -Force -ea silent
}
但是我得到同样的错误(我认为如果我尝试这个,这可能会跳过if
正文。
然后我尝试了
Set-ExecutionPolicy -Scope MachinePolicy Unrestricted
但我收到此错误:
Cannot set execution policy. Execution policies at the MachinePolicy or UserPolicy scopes must be set through Group
Policy.
但我不会在我的家庭系统上使用策略或任何与 AD 相关的内容。
Get-ExecutionPolicy -list
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser RemoteSigned
LocalMachine RemoteSigned
如果未设置策略,如何运行Set-Execution
,如果未设置,则跳过该策略?
如果未指定默认范围,则默认范围为LocalMachine
。出现该消息是因为CurrentUser
优先于LocalMachine
。一种检查方法是:
# [optional] temporarily suppress execution policy warnings
$E = $ErrorActionPreference
$ErrorActionPreference = 'SilentlyContinue'
if ((Get-ExecutionPolicy -Scope LocalMachine) -ne "RemoteSigned") {
# will always error if CurrentUser scope is set already
Set-ExecutionPolicy RemoteSigned -Scope LocalMachine -Force
}
if ((Get-ExecutionPolicy -Scope CurrentUser) -ne "RemoteSigned") {
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
}
$ErrorActionPreference = $E
由于某种原因,警告直接写入控制台,因此无法正常禁止显示警告。
或者,您可以仅设置CurrentUser
范围。如果不使用组策略,则只需担心三个作用域。最高的优先(设置较低的将显示警告):
Process
:仅为当前进程Set-ExecutionPolicy RemoteSigned -Scope Process
设置CurrentUser
:仅为当前用户设置:Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
LocalMachine
:为所有用户设置:Set-ExecutionPolicy RemoteSigned
有关更多信息,请查看about_Execution_Policies