繁体   English   中英

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

[英]Why is my locally-created script not allowed to run under the RemoteSigned execution policy?

由于此问题继续吸引被问题主体驳斥或未解决实际问题的回答,请阅读以下您需要了解的简单摘要

  • 不是“为什么我的 PowerShell 的默认安装不运行脚本?” 问题。
  • 不是“为什么我安装的 PowerShell 不能运行从 Internet 下载的脚本?” 问题。
  • 问题是为什么RemoteSigned执行策略在不应该阻止脚本执行时阻止脚本执行。
  • RemoteSigned是我要使用的唯一执行策略。 我知道还有其他限制较少的政策可用。 如果这些政策是可接受的替代品,我会直接使用它们,而这个问题将不存在。
  • 执行策略设置为RemoteSigned 将其从RemoteSigned更改为RemoteSigned不是解决方案。
  • 脚本文件在本地创建和存储。
  • 脚本文件未被阻止。 脚本文件从未被阻止(参见前一点)。
  • 脚本文件不能被解除阻塞,因为没有什么可以解除阻塞(见前一点)。
  • 脚本文件由管理员(试图)执行。
  • Windows PowerShell是唯一涉及的应用程序。 Windows PowerShell ISECommand Prompt或任何其他工具或编辑器无关。
  • 问题的原因已经确定(见接受的答案)。 近 8 年后,我认为所有其他明显的解释,无论是否适用,都已发布。 如果您不这么认为,请在添加之前完整阅读问题和现有答案。

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

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 ,我可以看到我为配置 PowerShell 而创建的Group Policy Object正在机器级别正确应用RemoteSigned执行策略:

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

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

正如about_Execution_Policies ( current ; at time of question ) 所记录的那样, RemoteSigned策略意味着:

  • 脚本可以运行。

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

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

  • 运行来自 Internet 以外来源的未签名脚本和已签名但恶意脚本的风险。

我的脚本没有签名,但由于它是在本地创建和执行的,它应该满足上面的第三个要点。 所以...

  • 为什么不允许我的脚本运行?
  • 为什么 PowerShell 抱怨我的脚本“没有经过数字签名”,而该要求只适用于来自 Internet 的文件?
  • 为什么 PowerShell 从网络共享运行时不再关心脚本未签名?

需要检查的一些事项:

可以改成无限制吗?

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

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

我在为 Dropbox 的映射驱动器运行 PS1 文件时发现,我总是收到此错误。 打开 PS1 的属性时,没有“解锁”。

唯一对我有用的是

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

当您运行 .ps1 PowerShell 脚本时,您可能会收到“.ps1 未进行数字签名”的消息。 该脚本不会在系统上执行。” 要修复它,您必须运行以下命令来运行 Set-ExecutionPolicy 并更改执行策略设置。

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

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

PS> & "$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. ...

PS> & "$Env:SystemRoot\Microsoft.NET\Framework64\v2.0.50727\caspol.exe" -machine -remgroup 1.2.3.
Microsoft (R) .NET Framework CasPol 2.0.50727.9136
Copyright (c) Microsoft Corporation.  All rights reserved.

The operation you are performing will alter security policy.
Are you sure you want to perform this operation? (yes/no)
yes
Removed code group from the Machine level.
Success

...我留下了默认的 CAS 配置,并且本地脚本现在可以再次工作。 自从我对 CAS 进行修补已经有一段时间了,我不确定为什么我的规则似乎会干扰那些将FullTrust授予MyComputer的人,但由于CAS 从 .NET 4.0 开始已被弃用(PowerShell 3.0 基于此) ,我想现在这是一个有争议的问题。

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

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

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

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

设置执行策略

模块:Microsoft.PowerShell.Security 为 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,获取%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/

对我有用的是右键单击 .ps1 文件,然后单击属性。 单击“解除锁定”按钮。 在花了几个小时试图改变政策后,我觉得效果很好。

选择您的终端命令提示符而不是 Power shell。 那应该行得通。

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

请备份 script.bs1 文件

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

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

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

Set-ExecutionPolicy -Scope Process 

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

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

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

  1. Set-ExecutionPolicy 不受限制

  2. Unblock-File -Path D:\PowerShell\Script.ps1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM