如何启用 PowerShell 脚本的执行?

Pav*_*uva 394 powershell

当我尝试执行 PowerShell 脚本时,出现此错误:

无法加载文件 C:\Common\Scripts\hello.ps1,因为在此系统上禁用了脚本的执行。有关更多详细信息,请参阅“get-help about_signing”。
在 line:1 char:13
+ .\hello.ps1 <<<<
+ CategoryInfo : NotSpecified: (:) [], PSSecurityException
+ fullyQualifiedErrorId : RuntimeException

Pav*_*uva 566

  1. 使用“以管理员身份运行”选项启动 Windows PowerShell。只有计算机上管理员组的成员才能更改执行策略。

  2. 通过输入启用运行未签名的脚本:

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

这将允许运行您在本地计算机上编写的未签名脚本和来自 Internet 的签名脚本。

另请参阅Microsoft TechNet 库中的运行脚本

  • 我讨厌有大约 15 个这样的答案,或者其他什么。这是非常危险的,而且一般来说是不受欢迎的事情。在末尾添加诸如“-Scope Process”之类的内容,这样就不会全局且永久地更改执行策略。如果是当前会话,那就足够了。即使有人经常使用此命令并且不记得该命令,也可以为答案添加书签 - 不要永远更改每个人的策略。 (11认同)
  • 这会永久更改策略还是每次重新启动计算机时都必须执行此操作? (4认同)
  • @Ray 这将永久更改政策。 (4认同)
  • 我会提到你需要这样做两次,在普通的电源外壳中,在`Windows PowerShell(x86)`中再一次。这是非常出乎意料的,但两者有不同的策略集。 (3认同)
  • @Ray 请参阅 [文档](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy)。默认情况下,它为“LocalMachine”设置它。要设置其他范围(`CurrentUser` 或`Process`),请显式传递`-Scope`。 (2认同)

Wil*_*sum 120

默认执行策略设置为受限,您可以通过运行Get-ExecutionPolicy来查看它:

Get-ExecutionPolicy
Run Code Online (Sandbox Code Playgroud)

像这样运行Set-ExecutionPolicy以切换到无限制模式:

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

  • 如果您希望用户从 Internet 复制和粘贴恶意脚本,则要求签名是有意义的。如果您假设用户并不愚蠢,那么“远程签名”不会增加任何安全性,并且会让生活变得困难。 (16认同)

MDM*_*313 75

在我用于开发脚本的机器上,我将使用 -unrestricted 如上所述。但是,在将我的脚本部署到最终用户计算机时,我将只使用 -executionpolicy 开关调用 powershell:

powershell.exe -noprofile -executionpolicy bypass -file .\script.ps1
Run Code Online (Sandbox Code Playgroud)

  • 您可能希望将此技巧与 .CMD 文件中的多语言技巧结合使用。见 http://stackoverflow.com/a/8597794/5314 (2认同)

小智 29

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

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

默认情况下它是Restricted。为了允许 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 下载的未签名脚本,则会在运行前提示您获得许可。

而在Bypass策略中,在脚本执行期间没有任何内容被阻止并且没有警告或提示。Bypass ExecutionPolicy 比 Unrestricted 更宽松。


Ben*_*hon 8

根据 Windows 版本和配置,即使在Unrestricted模式下,您也可能收到以下警告:

Security warning
Run only scripts that you trust. While scripts from the internet can be useful, this
script can potentially harm your computer. If you trust this script, use the 
Unblock-File cmdlet to allow the script to run without this warning message. 
Do you want to run?
[D] Do not run  [R] Run once  [S] Suspend  [?] Help (default is "D")
Run Code Online (Sandbox Code Playgroud)

解决方案是使用“绕过”策略,通过以下命令启用:

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

文档

绕过:没有被阻止,也没有警告或提示。

这显然是不安全的,请理解所涉及的风险。


小智 5

一个 .reg 文件:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell]
"EnableScripts"=dword:00000001 "ExecutionPolicy"="Bypass"
Run Code Online (Sandbox Code Playgroud)

和:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell]
"EnableScripts"=dword:00000001 "ExecutionPolicy"="Unrestricted"
Run Code Online (Sandbox Code Playgroud)

确实也有效。