首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PowerShell,只有在尚未设置的情况下才运行set执行?

PowerShell,只有在尚未设置的情况下才运行set执行?
EN

Stack Overflow用户
提问于 2022-10-11 18:21:36
回答 1查看 52关注 0票数 1

我有一个脚本试图运行这些..。

代码语言:javascript
运行
复制
Set-ExecutionPolicy -scope CurrentUser RemoteSigned -Force -ea silent
Set-ExecutionPolicy RemoteSigned -Force -ea silent

但我知道这个错误:

代码语言:javascript
运行
复制
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.

所以我试了一下:

代码语言:javascript
运行
复制
if ($(Get-ExecutionPolicy) -ne "RemoteSigned") {
    Set-ExecutionPolicy -scope CurrentUser RemoteSigned -Force -ea silent
    Set-ExecutionPolicy RemoteSigned -Force -ea silent
}

但是我也得到了同样的错误(我认为如果我尝试这样做,可能会跳过if主体。

然后我试着

代码语言:javascript
运行
复制
Set-ExecutionPolicy -Scope MachinePolicy Unrestricted

但我知道这个错误:

代码语言:javascript
运行
复制
Cannot set execution policy. Execution policies at the MachinePolicy or UserPolicy scopes must be set through Group
Policy.

但在我的家庭系统里,我不使用任何政策或任何与广告相关的东西。

代码语言:javascript
运行
复制
Get-ExecutionPolicy -list

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    RemoteSigned
 LocalMachine    RemoteSigned

如果没有设置策略,我如何运行Set-Execution?如果没有设置策略,如何跳过它?

EN

Stack Overflow用户

回答已采纳

发布于 2022-10-11 19:20:31

如果您不指定LocalMachine,默认作用域是一个。出现此消息是因为CurrentUser优先于LocalMachine。检查的一种方法是:

代码语言:javascript
运行
复制
# [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作用域。如果您没有使用组策略,那么只需要担心三个作用域。最高的优先(设置较低的将显示警告):

  1. Process:只为当前进程Set-ExecutionPolicy RemoteSigned -Scope Process设置
  2. CurrentUser:只为当前用户设置:Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
  3. LocalMachine:为所有用户设置:Set-ExecutionPolicy RemoteSigned

有关更多信息,请访问政策

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74032506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档