PowerShell说"在这个系统上禁用了脚本的执行."

Con*_*nor 1545 powershell windows-server-2008-r2

我正在尝试运行从命令提示符调用PowerShell脚本的.cmd文件,我收到以下错误:

无法加载Management_Install.ps1,因为在此系统上禁用了脚本的执行.

我跑了cmd.exe,当我Management_Install.ps1从PowerShell 运行时,我Get-ExecutionPolicy回来了.

// Powershell的输出

PS C:\ Users\Administrator> get-executionpolicy

无限制

//从DOS输出

C:\ Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scr

ipts> powershell.\ Management_Install.ps1 1

警告:运行x86 PowerShell ...

无法加载文件C:\ Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts\Management_Install.ps1,因为在此系统上禁用了脚本的执行.有关详细信息,请参阅"get-help about_signing".

在线:1个字符:25

  • .\ Management_Install.ps1 <<<< 1

    • CategoryInfo:NotSpecified:(:) [],PSSecurityException

    • FullyQualifiedErrorId:RuntimeException

C:\ Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts> pause

按任意键继续 ...

该系统是Windows Server 2008 R2.

我究竟做错了什么?

Cha*_*ler 1992

如果您使用的是Windows Server 2008 R2,那么有一个x64x86版本的PowerShell,这两个版本都必须设置其执行策略.您是否在两台主机上设置了执行策略?

作为管理员,您可以通过在PowerShell窗口中键入以下内容来设置执行策略:

Set-ExecutionPolicy RemoteSigned
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅使用Set-ExecutionPolicy Cmdlet.

  • 如果你想将权限恢复原状,那么`Set-ExecutionPolicy Restricted`似乎是撤消它的方法:http://technet.microsoft.com/en-us/library/ee176961.aspx.`@Jack Edmonds`的临时旁路方法对我来说看起来更安全:`powershell -ExecutionPolicy ByPass -File script.ps1` (130认同)
  • 要获得更安全的策略,请将其范围限定为实际用户:Set-ExecutionPolicy RemoteSigned -Scope CurrentUser (22认同)

Jac*_*nds 641

您可以通过-ExecutionPolicy ByPass在运行PowerShell时添加来绕过此策略

powershell -ExecutionPolicy ByPass -File script.ps1
Run Code Online (Sandbox Code Playgroud)

  • 执行策略的目的是防止人们双击`.ps1`并意外地运行他们不想要的东西.使用`.bat`文件会发生这种情况 (7认同)
  • 如果您使用的是非管理员帐户,这也非常方便.我在任务栏上创建了`%SystemRoot%\ system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass`的快捷方式. (3认同)
  • 请注意,Microsoft Technet将其样式化为“绕过”,而不是“绕过”。请参阅:https://technet.microsoft.com/nl-nl/library/hh849812.aspx (2认同)
  • 这对我不起作用,我得到的许可被拒绝,就像我正常调用它一样。通过执行`type script.ps1 | 从.bat 调用ps1 | powershell -` 确实有效。 (2认同)
  • `找不到与参数名称“文件”匹配的参数`命令:`Set-ExecutionPolicy -ExecutionPolicy Bypass -File .\file.ps1` (2认同)

Ral*_*oss 131

我遇到了类似的问题,并注意到Windows Server 2012cmd上的默认设置是运行x64.

对于Windows 7,Windows 8,Windows Server 2008 R2Windows Server 2012,请以管理员身份运行以下命令:

x86(32位)
打开C:\Windows\SysWOW64\cmd.exe
运行命令powershell Set-ExecutionPolicy RemoteSigned

x64(64位)
打开C:\Windows\system32\cmd.exe
运行命令powershell Set-ExecutionPolicy RemoteSigned

您可以使用检查模式

  • 在CMD中: echo %PROCESSOR_ARCHITECTURE%
  • 在Powershell中: [Environment]::Is64BitProcess

参考:
MSDN - Windows PowerShell执行策略
Windows - 32位对64位目录说明

  • 在 windows10 中适合我 (4认同)

Kyl*_*Mit 113

大多数现有答案都解释了How,但很少解释为什么.在你从互联网上的陌生人执行代码之前,特别是禁用安全措施的代码之前,你应该完全理解你在做什么.所以这里有关于这个问题的更多细节.

从TechNet 关于执行策略页面:

通过Windows PowerShell执行策略,您可以确定Windows PowerShell加载配置文件和运行脚本的条件.

PowerShell Basics - 执行策略和代码签名所列举的好处包括:

  • 控制执行 - 控制执行脚本的信任级别.
  • 命令Highjack - 防止在我的路径中注入命令.
  • 身份 - 脚本是由我信任的开发人员创建和签名的,和/或使用我信任的证书颁发机构的证书签名.
  • 完整性 - 恶意软件或恶意用户无法修改脚本.

要检查当前的执行策略,您可以运行Get-ExecutionPolicy.但你可能在这里,因为你想改变它.

为此,您将运行Set-ExecutionPolicycmdlet.

在更新执行策略时,您将做出两个重大决策.

执行政策类型:

  • Restricted - 无法在系统上执行本地,远程或下载的脚本.
  • AllSigned - 所有运行的脚本都需要进行数字签名.
  • RemoteSigned - 需要签署所有远程脚本(UNC)或下载的脚本.
  • Unrestricted - 不需要任何类型脚本的签名.

新变化的范围

  • LocalMachine - 执行策略会影响计算机的所有用户.
  • CurrentUser - 执行策略仅影响当前用户.
  • Process - 执行策略仅影响当前的Windows PowerShell进程.

†=默认

例如:如果您只想为CurrentUser将策略更改为RemoteSigned,则运行以下命令:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Run Code Online (Sandbox Code Playgroud)

注意:要更改执行策略,您必须运行PowerShell As Adminstrator.如果您处于常规模式并尝试更改执行策略,则会收到以下错误:

访问注册表项"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell"被拒绝.若要更改默认(LocalMachine)范围的执行策略,请使用"以管理员身份运行"选项启动Windows PowerShell.

如果要加强对尚未从Internet下载的脚本的内部限制(或至少不包含UNC元数据),可以强制策略仅运行签名的sripts.要签署自己的脚本,可以按照Scott Hanselman关于签署PowerShell脚本的文章中的说明进行操作.

注意:大多数人在打开Powershell时都可能会收到此错误,因为PS在启动时尝试做的第一件事是执行您的用户配置文件脚本,根据您的喜好设置您的环境.

该文件通常位于:

%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
Run Code Online (Sandbox Code Playgroud)

您可以通过运行powershell变量找到确切的位置

$profile
Run Code Online (Sandbox Code Playgroud)

如果配置文件中没有您关心的任何内容,并且不想对您的安全设置大惊小怪,您可以删除它,而PowerShell将找不到任何无法执行的操作.

  • 我觉得重要的是要注意,虽然执行策略是一种安全措施,但并不是要阻止用户执行PowerShell代码.执行策略旨在*保护您的脚本*并确定谁编写,修改或批准它们,这就是全部.使用像Get-Content.\ MyFile这样简单的东西来解决执行政策(https://blog.netspi.com/15-ways-to-bypass-the-powershell-execution-policy/)是微不足道的. ps1 | powershell.exe -NoProfile -`. (7认同)
  • 考虑到 `-ExecutionPolicy ByPass` 的存在,无论如何,这个策略的目的是什么?是否只是为了防止用户不小心打开powershell 控制台并运行恶意脚本?如果攻击者想解决这个问题,难道他们不能只使用可执行文件或批处理脚本吗?即使在阅读@BaconBits 评论后,我也不太确定该政策旨在防止什么情况...... (3认同)
  • @ Ajedi32说我有一个在网络共享上运行脚本的任务.当我调用我的脚本时,我希望我的进程验证脚本是否已签名.我希望我的代码能够仔细检查我要运行的脚本是否是我信任的代码.我不在乎你是否可以运行我的代码.停止这是访问权限的工作.我只是想阻止你让我运行我没写过的代码.访问权限意味着操作系统会阻止您在登录时修改我的代码.代码签名和执行策略意味着我的脚本在*I*运行时未被修改. (2认同)
  • 展示如何签署脚本而不是如何禁用安全性怎么样?这是一个选择吗? (2认同)

Rya*_*yan 37

在Windows 7中:

转到"开始"菜单,然后搜索"Windows PowerShell ISE".

右键单击x86版本,然后选择"以管理员身份运行".

在顶部,粘贴Set-ExecutionPolicy RemoteSigned; 运行脚本.选择"是".

对64位版本的Powershell ISE也重复这些步骤(非x86版本).

我只是澄清了@Chad Miller所暗示的步骤.谢谢乍得!


小智 36

在脚本之前运行此命令也可以解决问题:

set-executionpolicy unrestricted
Run Code Online (Sandbox Code Playgroud)

  • -1 - 遵循最少许可原则.在删除对安全策略的所有限制之前,至少将策略设置为"RemoteSigned".如果这不起作用,那么重新评估你的痛点是什么以及它为什么不起作用.你可以设置`unrestricted`作为最后的手段,但它不应该是你的起点. (25认同)
  • 谢谢你指出这个选项.由于对生产目的的安全需求充分考虑,在快速原型制作能力需求如此之高的时代,所有的策略和安全性确实妨碍了完成工作. (3认同)

gho*_*ade 36

首先,您需要打开 PowerShell 窗口并运行此命令。

set-ExecutionPolicy RemoteSigned -Scope CurrentUser

然后它会要求你确认。键入Y并按Enter

当您运行此命令时,您可以看到您的系统已将当前用户的所有策略设置为远程。完成此过程将需要几秒钟的时间。

图像将如下所示:

在此输入图像描述

检查执行策略是否已设置。类型:

Get-ExecutionPolicy

如果设置了,输出将如下所示:

在此输入图像描述


小智 31

如果您所处的环境不是管理员,则可以仅为您设置执行策略,并且不需要管理员.

Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
Run Code Online (Sandbox Code Playgroud)

要么

Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted"
Run Code Online (Sandbox Code Playgroud)

您可以在帮助条目中阅读所有相关信息.

Help Get-ExecutionPolicy -Full
Help Set-ExecutionPolicy -Full
Run Code Online (Sandbox Code Playgroud)

  • Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted" 是唯一对我有用的解决方案。谢谢 (2认同)

小智 27

RemoteSigned:您自己创建的所有脚本都将运行,从Internet下载的所有脚本都需要由受信任的发布者签名.

好的,只需输入以下内容即可更改策略:

Set-ExecutionPolicy RemoteSigned
Run Code Online (Sandbox Code Playgroud)


Arv*_*iya 27

打开 Windows PowerShell 命令窗口并运行以下查询进行更改ExecutionPolicy

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

如果要求确认更改,请按Y并点击Enter


Pra*_*til 26

我们可以ExecutionPolicy通过以下命令获取当前状态:

Get-ExecutionPolicy;
Run Code Online (Sandbox Code Playgroud)

默认情况下,它是受限制的.要允许执行PowerShell脚本,我们需要将此ExecutionPolicy设置为BypassUnrestricted.

我们可以将当前用户的策略设置为BypassUnrestricted使用以下任何PowerShell命令:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force;
Run Code Online (Sandbox Code Playgroud)

不受限制的策略加载所有配置文件并运行所有脚本.如果您运行从Internet下载的未签名脚本,则会在运行之前提示您获得权限.

绕过策略中,没有任何内容被阻止,并且在脚本执行期间没有警告或提示.绕道ExecutionPolicy比放松更轻松Unrestricted.

  • `Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;`AKA快速而又脏的方式告诉VS2015停止抱怨并运行我的血腥脚本.谢谢.救星. (3认同)

MD *_*YON 24

  1. 以管理员身份打开 powershell
Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
Run Code Online (Sandbox Code Playgroud)

使用这个命令


小智 21

你应该运行这个命令:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
Run Code Online (Sandbox Code Playgroud)

  • 这与_八年前_的[@Micah 'Powershell Ninja'的答案](/sf/answers/1405605421/)_完全_相同。而且,就此而言,七年前的[@KyleMit 的特殊接受答案](/sf/answers/1886853531/)有效地涵盖了它。在提交新答案之前,请查看现有答案并对您认为有用的答案进行投票。_仅当您有**新的且实质性的**内容可以添加到现有答案中时,才对已确定的问题提交新答案_。 (4认同)

小智 19

我正在使用Windows 10,无法运行任何命令.给我一些线索的唯一命令是这样的:

[64]

  1. 以管理员身份打开C:\ Windows\SysWOW64\cmd.exe
  2. 运行命令> powershell Set-ExecutionPolicy Unrestricted

但这没效果.它很有限.可能是Windows10的新安全策略.我有这个错误:

Set-ExecutionPolicy:Windows PowerShell已成功更新执行策略,但该设置被更具体范围内定义的策略覆盖.由于覆盖,您的shell将保留其当前有效的执行策略...

所以我找到了另一种方式(解决方案):

  1. 打开运行命令/控制台(Win+ R)
  2. 键入:gpedit.msc(组策略编辑器)
  3. 浏览到本地计算机策略 - > 计算机配置 - > 管理模板 - > Windows组件 - > Windows Powershell.
  4. 启用" 打开脚本执行 "
  5. 根据需要设置策略.我将我设置为" 允许所有脚本 ".

现在打开PowerShell并享受;)


php*_*erd 14

在 Windows 中打开命令提示符。如果问题仅与 PowerShell 相关,请使用以下命令:

powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
Run Code Online (Sandbox Code Playgroud)


小智 13

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
Run Code Online (Sandbox Code Playgroud)

这对我有用


小智 11

你可以试试这个并选择“全部”选项

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
Run Code Online (Sandbox Code Playgroud)


小智 10

  1. 打开运行命令/控制台 (Win + R) 输入:gpedit。msc(组策略编辑器)
  2. 浏览到本地计算机策略->计算机配置->管理模板-> Windows 组件-> Windows Powershell。
  3. 启用打开脚本执行”根据需要设置策略。我将我的设置为“允许所有脚本”

现在运行你正在使用的运行命令..相信这个应用程序会运行..享受:)


小智 9

设置执行策略是特定于环境的.如果您尝试从正在运行的x86 ISE执行脚本,则必须使用x86 PowerShell来设置执行策略.同样,如果您运行的是64位ISE,则必须使用64位PowerShell设置策略.


Ras*_*yal 9

我也遇到过类似的问题试试这个希望它对某人有所帮助 因为我使用的是 windows 所以按照下面给出的步骤以管理员身份打开命令提示符然后转到此路径

C:\Users\%username%\AppData\Roaming\npm\
Run Code Online (Sandbox Code Playgroud)

在此文件夹(dir)中查找文件ng.ps1,然后将其删除(del ng.ps1)

您也可以在此之后清除 npm 缓存,尽管它也可以在没有此步骤的情况下工作。希望它对我有用,因为它对我有用。

希望能帮助到你


小智 9

如果您安装了 Git,则只需使用Git Bash

在此输入图像描述


Ale*_*nno 8

您还可以使用以下命令绕过此操作:

PS > powershell Get-Content .\test.ps1 | Invoke-Expression
Run Code Online (Sandbox Code Playgroud)

您还可以阅读Scott Sutherland撰写的这篇文章,其中介绍了Set-ExecutionPolicy在没有管理员权限的情况下绕过PowerShell的15种不同方法:

15种绕过PowerShell执行策略的方法


Qam*_*mar 8

Win+ R并键入copy paste命令并按OK:

powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
Run Code Online (Sandbox Code Playgroud)

并执行您的脚本.

然后还原更改,如:

powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "AllSigned"
Run Code Online (Sandbox Code Playgroud)


mkl*_*nt0 8

现有答案中有很多信息,但让我尝试系统概述

语境

PowerShell 的有效执行策略适用

  • 存储在文件中的 PowerShell 代码,这意味着常规脚本文件 ( .ps1)、脚本模块文件 ( .psm1) 以及格式类型扩展文件 ( .psm1xml)。

    • 它不适用于
      • 调用(二进制)cmdlet(例如,Get-ChildItem
      • 以交互方式提交的命令或通过参数传递到 PowerShell CLI 的
        -Command命令(除非这些命令直接或间接调用上面定义的脚本文件)。
  • 仅在 Windows 上(即,在类 Unix平台(Linux、macOS)上,执行策略不适用,并且对执行 PowerShell 代码没有任何限制)

在Windows 的工作站版本上,默认情况下禁用脚本文件执行(策略Restricted),需要对策略进行持久修改才能启用它,或者仅对当前进程进行修改,例如-ExecutionPolicy在调用 PowerShell CLIpowershell.exe时通过参数进行修改(Windows PowerShell版)/ pwsh.exePowerShell(核心)版)。

执行策略是单独维护的:

  • 对于两个PowerShell版本

    • 旧版、仅限 Windows、Windows 附带的Windows PowerShell版本(其最新版本是 v5.1.x)
    • the modern, cross-platform, install-on-demand PowerShell (Core) edition (v6+).
  • for the 32-bit and 64-bit versions of Windows PowerShell (both of which are preinstalled)

    • Note: If you were to install 32-bit and 64-bit versions of PowerShell (Core) side by side (which would be unusual), only the LocalMachine scope would be bitness-specific.

For a given edition / bitness combination of PowerShell, the execution policies can be set in multiple scopes, but there's only ever one effective policy, based on precedence rules - see below.

Details

  • In PowerShell on Windows, script-file execution is disabled by default in workstation editions of Windows (on Unix, execution policies do not apply); that is, the default execution policy in workstation editions of Windows is Restricted, whereas in server editions, it is RemoteSigned; see the conceptual about_Execution_Policies help topic for a description of all available policies.

  • To set a (local) policy that permits script execution, use Set-ExecutionPolicy. There are three scopes that Set-ExecutionPolicy can target, using the -Scope parameter (see below); changing the LocalMachine scope requires elevation (running as admin).

    • To unset a previously set policy in a given scope, use Undefined
  • A frequently used policy that provides a balance between security and convenience is RemoteSigned, which allows local scripts - including from network shares - to execute without containing a signature, while requiring scripts downloaded from the internet to be signed (assuming that the downloading mechanism marks such as scripts as internet-originated, which web browsers do by default). For instance, to set the current user's execution policy to RemoteSigned, run the following:

    Set-ExecutionPolicy -Scope CurrentUser RemoteSigned -Force
    
    Run Code Online (Sandbox Code Playgroud)
  • The PowerShell CLI (powershell.exe for Windows PowerShell, pwsh.exe for PowerShell (Core), v6+) accepts a process-specific -ExecutionPolicy <policy> argument too, which is often used for ad-hoc policy overrides (only for the process being created, the equivalent of Set-ExecutionPolicy -Scope Process ..); e.g.:

    pwsh.exe -ExecutionPolicy RemoteSigned -File someScript.ps1
    
    Run Code Online (Sandbox Code Playgroud)
  • Important:

    • 执行策略也可以通过组策略对象 (GPO)设置,在这种情况下,它们不能使用CLI进行更改或覆盖Set-ExecutionPolicy:请参阅about_Group_Policy_Settings

    • 执行策略可以在不同的作用域中设置,哪一个有效是由它们的优先级决定的(运行以查看所有作用域及其各自的策略),按降序排列Get-ExecutionPolicy -List

      • MachinePolicy(通过 GPO;无法在本地覆盖)[1]
      • UserPolicy(通过 GPO;无法在本地覆盖)[1]
      • Process(仅限当前进程;通常通过 CLI 设置临时进程)
      • CurrentUser(由 设定Set-ExecutionPolicy
      • LocalMachine(由 设定Set-ExecutionPolicy,具有管理员权限)

[1] 这适用于范围内的 GPO。本地GPO可以 在本地修改,即通过gpedit.msc或直接通过注册表进行修改。


Dav*_*las 7

在 PowerShell ISE编辑器中,我发现运行以下行首先允许脚本。

Set-ExecutionPolicy RemoteSigned -Scope Process
Run Code Online (Sandbox Code Playgroud)


Jef*_*ong 7

在窗口 10 中:

如果您不是管理员,您可以使用以下命令:

powershell Set-ExecutionPolicy -Scope CurrentUser

cmdlet Set-ExecutionPolicy at command pipeline position 1
Supply values for the following parameters:
ExecutionPolicy: `RemoteSigned`
Run Code Online (Sandbox Code Playgroud)

它像魅力一样解决了我的问题!


小智 7

对于 Windows 11...

这确实很容易。只需打开设置应用程序即可。导航至隐私和安全

隐私和安全图像

单击“对于开发人员”并滚动到底部,找到 PowerShell 选项,在该选项下选中“更改执行策略...远程脚本”复选框。

开发者选项图片


Use*_*ser 6

Set-ExecutionPolicy RemoteSigned
Run Code Online (Sandbox Code Playgroud)

在powershell中以管理员模式执行此命令即可解决问题。


Ram*_*lam 5

  1. 以管理员身份打开PowerShell并运行Set-ExecutionPolicy -Scope CurrentUser
  2. 提供RemoteSigned并按Enter键
  3. 运行Set-ExecutionPolicy -Scope CurrentUser
  4. 提供Unrestricted并按Enter键


Bib*_*bin 5

在 powershell 中

要检查当前的执行策略,请使用以下命令:

Get-ExecutionPolicy

要将执行策略更改为“无限制”(允许运行任何没有数字签名的脚本),请使用以下命令:

Set-ExecutionPolicy Unrestricted

这个解决方案对我有用,但要小心所涉及的安全风险。