gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:45:04 26 4
gpt4 key购买 nike

Since this question continues to attract responses that are eitherrefuted by the question body or don't address the actual problem,please read this simple summary of what you need to know:

  • This is not a "Why won't my default installation of PowerShell run scripts?" question.
  • This is not a "Why won't my installation of PowerShell run scripts downloaded from the internet?" question.
  • The question is why the RemoteSigned execution policy is preventing script execution when it shouldn't.
  • RemoteSigned is the only execution policy I want to use. I am aware that other, less-restrictive policies are available. Ifthose policies were acceptable substitutes I would have just used theminstead and this question wouldn't exist.
  • The execution policy is already set to RemoteSigned. Changing it from RemoteSigned to RemoteSigned is not a solution.
  • The script file is created and stored locally.
  • The script file is not blocked. The script file was never blocked (see previous point).
  • The script file cannot be unblocked because there is nothing to unblock (see previous point).
  • The script file is (attempted to be) executed by an administrator.
  • Windows PowerShell is the only application involved. Not Windows PowerShell ISE nor Command Prompt nor any other tools oreditors are relevant.
  • The cause of the problem has already been identified (see accepted answer). After nearly 8 years, I think all other obviousexplanations, whether applicable or not, have been posted, too. Ifyou think otherwise then please read the question and existinganswers in their entirety before adding yours.
<小时/>

我在 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 ,我可以看到我创建的用于配置 PowerShell 的组策略对象正确应用了 RemoteSigned机器级别的执行策略:

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

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

about_Execution_Policies 所记录(currentat time of question),RemoteSigned政策手段:

  • Scripts can run.

  • Requires a digital signature from a trusted publisher on scripts and configuration files that are downloaded from the Internet(including e-mail and instant messaging programs).

  • Does not require digital signatures on scripts that you have run and that you have written on the local computer (not downloaded fromthe Internet).

  • Risks running unsigned scripts from sources other than the Internet and signed, but malicious, scripts.

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

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

最佳答案

一些需要检查的事情:

可以改为无限制吗?

Set-ExecutionPolicy Unrestricted

组策略设置了吗?

  • 计算机配置\管理模板\Windows 组件\Windows PowerShell
  • 用户配置\管理模板\Windows 组件\Windows PowerShell

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

这允许它运行吗?

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

关于powershell - 为什么我本地创建的脚本不允许在 RemoteSigned 执行策略下运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9742775/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com