为什么我的本地创建的脚本不允许在RemoteSigned执行策略下运行?

时间:2012-03-16 18:40:00

标签: powershell powershell-v2.0 code-signing

我在64位Windows 7 Professional上使用Windows PowerShell 2.0。我的桌面上有一个脚本,当我尝试运行它时会导致以下错误:

File C:\Users\UserName\Desktop\Script.ps1 cannot be loaded. The file C:\Users\UserName\Desktop\Script.ps1 is not digitally signed. The script will not execute on the system.  Please see "get-help about_signing" for more details..
At line:1 char:54
+ C:\Users\UserName\Desktop\TestGetWindowsUpdateLog.ps1 <<<<
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

我既是域管理员又是本地管理员,如果我运行Get-ExecutionPolicy -List,我可以看到我为配置Group Policy Object创建的PowerShell正确应用{{1}在机器级别执行策略:

RemoteSigned

我自己在Notepad创建了该脚本,并使用Sysinternals'streams实用程序和文件 Scope ExecutionPolicy ----- --------------- MachinePolicy RemoteSigned UserPolicy Undefined Process Undefined CurrentUser Undefined LocalMachine Undefined 对话框确认脚本未被视为具有来自互联网。如果我将脚本复制到域服务器上的网络共享,则允许执行。如果我运行Properties,那么仍然不允许执行本地脚本,这是有道理的,因为Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope LocalMachine范围的执行策略将优先。

正如about_Execution_Policies所述,MachinePolicy政策意味着:

  • 脚本可以运行。

  • 需要来自受信任的发布者的数字签名,用于从Internet下载的脚本和配置文件(包括电子邮件和即时消息程序)。

  • 在您运行的脚本上以及您在本地计算机上编写的脚本(不是从Internet下载)上不需要数字签名。

  • 从Internet以外的来源运行未签名的脚本并签名但是恶意的脚本。

我的脚本没有签名,但由于它是在本地创建和执行的,因此它应满足上面的第三个要点。那么为什么它不被允许运行?为什么当该要求仅适用于来自Internet的文件时,PowerShell会抱怨我的脚本“未经过数字签名”?为什么它不再关心脚本在从网络共享运行时没有被签名?

15 个答案:

答案 0 :(得分:76)

文件被阻止了吗?我有同样的问题,可以通过右键单击.PS1文件,属性并选择取消阻止来解决它。

答案 1 :(得分:43)

要检查的一些事项:

你可以改为不受限制吗?

Set-ExecutionPolicy Unrestricted

是否已设置组策略?

  • Computer Configuration\Administrative Templates\Windows Components\Windows PowerShell
  • User Configuration\Administrative Templates\Windows Components\Windows PowerShell

另外,你是如何调用Script.ps1的?

这是否允许它运行?

powershell.exe -executionpolicy bypass -file .\Script.ps1

答案 2 :(得分:7)

我终于将其追溯到.NET Code Access Security。我有一些内部开发的二进制模块,存储在网络共享上并从网络共享中执行。为了让.NET 2.0 / PowerShell 2.0加载它们,我在Intranet代码组中添加了一个URL规则来信任该目录:

PS C:\Users\UserName> & "$Env:SystemRoot\Microsoft.NET\Framework64\v2.0.50727\caspol.exe" -machine -listgroups
Microsoft (R) .NET Framework CasPol 2.0.50727.5420
Copyright (c) Microsoft Corporation.  All rights reserved.

Security is ON
Execution checking is ON
Policy change prompt is ON

Level = Machine

Code Groups:

1.  All code: Nothing
    1.1.  Zone - MyComputer: FullTrust
        1.1.1.  StrongName - ...: FullTrust
        1.1.2.  StrongName - ...: FullTrust
    1.2.  Zone - Intranet: LocalIntranet
        1.2.1.  All code: Same site Web
        1.2.2.  All code: Same directory FileIO - 'Read, PathDiscovery'
        1.2.3.  Url - file://Server/Share/Directory/WindowsPowerShell/Modules/*: FullTrust
    1.3.  Zone - Internet: Internet
        1.3.1.  All code: Same site Web
    1.4.  Zone - Untrusted: Nothing
    1.5.  Zone - Trusted: Internet
        1.5.1.  All code: Same site Web

请注意,根据安装的.NET版本以及是否为32位或64位Windows,caspol.exe可以存在于以下位置,每个位置都有自己的安全配置(security.config ):

  • $Env:SystemRoot\Microsoft.NET\Framework\v2.0.50727\
  • $Env:SystemRoot\Microsoft.NET\Framework64\v2.0.50727\
  • $Env:SystemRoot\Microsoft.NET\Framework\v4.0.30319\
  • $Env:SystemRoot\Microsoft.NET\Framework64\v4.0.30319\

删除组1.2.3(让我使用CAS的默认配置)后,本地脚本现在再次运行。我已经用CAS修改了一段时间,而且我不确定为什么我的规则似乎会干扰那些授予FullTrustMyComputer但是从CAS is deprecated as of .NET 4.0开始的那些规则。 PowerShell 3.0是基于),我想现在这是一个没有实际意义的点。

答案 3 :(得分:4)

如果文件是从网络位置(即另一台计算机)复制的,则Windows可能已阻止该文件。右键单击该文件,然后单击“取消阻止”按钮,看它是否有效。

答案 4 :(得分:3)

此代码行在 Ubuntu 中使用 virtualbox 和 Win 10 作为虚拟操作系统,在 powershell 内部

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

如果它不起作用,我建议阅读以下资源:

设置执行策略

模块: Microsoft.PowerShell.安全 为 Windows 计算机设置 PowerShell 执行策略。

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.1#:~:text=To%20change%20the%20execution%20policy,Get%2DExecutionPolicy%20with%20no%20parameters

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-executionpolicy?view=powershell-7.1

Get-ExecutionPolicy -List

https://devblogs.microsoft.com/scripting/hey-scripting-guy-how-can-i-sign-windows-powershell-scripts-with-an-enterprise-windows-pki-part-2-of-2/

答案 5 :(得分:1)

这是一个IDE问题。更改PowerShell GUI中的设置。转到工具选项卡,选择选项,然后选择调试选项。然后选中关闭要签名的脚本的要求框。完成。

答案 6 :(得分:0)

尝试以管理员身份运行Powershell GUI

答案 7 :(得分:0)

请备份script.bs1文件

对我有用的是删除script.bs1文件并运行执行命令。

答案 8 :(得分:-1)

对我有用的是右键单击.ps1文件,然后右键单击属性。点击&#34; UNBLOCK&#34;按钮。花了好几个小时试图改变政策后,我的工作非常棒。

答案 9 :(得分:-1)

运行PS1文件到Dropbox的映射驱动器时,我一直发现此错误。打开PS1的属性时,没有“取消阻止”。

唯一对我有用的是

powershell.exe -executionpolicy旁路-file。\ Script.ps1

答案 10 :(得分:-1)

选择终端的命令提示符而不是Power Shell。应该可以。

答案 11 :(得分:-1)

运行.ps1 PowerShell脚本时,您可能会收到消息“ .ps1没有经过数字签名。该脚本将无法在系统上执行。” 要解决此问题,您必须运行下面的命令以运行Set-ExecutionPolicy并更改执行策略设置。

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

答案 12 :(得分:-1)

发生这种情况是因为 Powershell 执行策略默认设置为受限,这会阻止执行 PowerShell 脚本并防止运行恶意脚本。

您可以通过运行以下命令来更改特定范围的执行范围

Set-ExecutionPolicy -Scope Process 

答案 13 :(得分:-2)

我遇到了同样的问题并通过更改默认程序将.ps1文件打开到PowerShell来修复它。它被设置为Notepad

答案 14 :(得分:-4)

在PowerShell窗口中运行以下2条命令

  1. Set-ExecutionPolicy不受限制

  2. 取消阻止文件-路径D:\ PowerShell \ Script.ps1

相关问题