我有一个脚本试图运行这些..。
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.
但在我的家庭系统里,我不使用任何政策或任何与广告相关的东西。
Get-ExecutionPolicy -list
Scope ExecutionPolicy
----- ---------------
MachinePolicy Undefined
UserPolicy Undefined
Process Undefined
CurrentUser RemoteSigned
LocalMachine RemoteSigned
如果没有设置策略,我如何运行Set-Execution
?如果没有设置策略,如何跳过它?
发布于 2022-10-11 19:20:31
如果您不指定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
有关更多信息,请访问政策
https://stackoverflow.com/questions/74032506
复制相似问题