ChatGPT解决这个技术问题 Extra ChatGPT

为什么我的 PowerShell 脚本没有运行?

我编写了一个简单的批处理文件作为 PowerShell 脚本,运行时出现错误。

它位于我路径中的脚本目录中。这是我得到的错误:

无法加载,因为在此系统上禁用了脚本的执行。请参阅“获取有关签名的帮助”。

我查看了帮助,但它没有帮助。


T
TylerH

它可能是 PowerShell 的默认安全级别,它 (IIRC) 将只运行签名脚本。

试着输入这个:

set-executionpolicy remotesigned

这将告诉 PowerShell 允许本地(即在本地驱动器上)未签名的脚本运行。

然后尝试再次执行您的脚本。


您必须以管理员权限运行 Powershell,至少在 Windows 8 下!
您还必须在 Windows 7 下以管理权限运行 PowerShell 脚本。
这是一个相当可怕的答案。 一方面,它永久以可能不受欢迎(和不安全)的方式更改 Powershell 的默认安全级别。另一方面,它甚至未能充分解释已签名的远程脚本和未签名的本地脚本——但not 未签名的远程脚本,Chocolatey 偶尔需要——将被授予执行权限。大多数用户可能想要 thisthis
虽然塞西尔对答案中的一些弱点的担忧是公平的,但我想指出一些事情。 1)他的两个链接似乎为我打开了同一个页面。 2)如果您希望脚本简单地运行,设置执行策略可能仍然是要走的路,并且可能与OP无关:3)似乎最好的策略是启动脚本,在命令行中指定执行策略和范围运行脚本,并根据需要为登录设置会话配置。如果您希望在 Windows 会话期间获得高安全性但降低登录时的安全性,您可能希望在脚本顺序中获得细微差别。
对于一次性问题,我发现为正在运行的 PowerShell 实例定义临时异常的 Ankur's answer below 最有帮助。
T
TylerH

您需要运行 Set-ExecutionPolicy

Set-ExecutionPolicy Restricted <-- Will not allow any powershell scripts to run.  Only individual commands may be run.

Set-ExecutionPolicy AllSigned <-- Will allow signed powershell scripts to run.

Set-ExecutionPolicy RemoteSigned <-- Allows unsigned local script and signed remote powershell scripts to run.

Set-ExecutionPolicy Unrestricted <-- Will allow unsigned powershell scripts to run.  Warns before running downloaded scripts.

Set-ExecutionPolicy Bypass <-- Nothing is blocked and there are no warnings or prompts.

Windows 将新安装设置为的默认 ExecutionPolicy 值是多少?
@MatthewLock Restricted 是默认策略。 Read More
P
Peter Mortensen

利用:

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

始终使用上述命令启用在当前会话中执行 PowerShell。


this + superuser.com/questions/612409/… + 快捷链接 = 完美
P
Peter Mortensen

我可以通过像这样调用 PowerShell 来绕过这个错误:

powershell -executionpolicy bypass -File .\MYSCRIPT.ps1

也就是说,我在调用脚本的方式中添加了 -executionpolicy bypass

这适用于 Windows 7 Service Pack 1。我是 PowerShell 新手,因此可能会有一些我不知道的警告。

[编辑 2017-06-26] 我继续在其他系统上使用此技术,包括 Windows 10 和 Windows 2012 R2,没有问题。

这是我现在正在使用的。这可以防止我通过单击意外运行脚本。当我在调度程序中运行它时,我添加了一个参数:“调度程序”并且绕过了提示。

这也会在最后暂停窗口,以便我可以看到 PowerShell 的输出。

if NOT "%1" == "scheduler" (
   @echo looks like you started the script by clicking on it.
   @echo press space to continue or control C to exit.
   pause
)

C:
cd \Scripts

powershell -executionpolicy bypass -File .\rundps.ps1

set psexitcode=%errorlevel%

if NOT "%1" == "scheduler" (
   @echo Powershell finished.  Press space to exit.
   pause
)

exit /b %psexitcode%

这是 chocolatey 用来下载和安装 Chocolatey 的工具
我尝试运行命令 Set-ExecutionPolicy bypass -File C:\Users\david\Desktop\test.ps1 并收到一条错误消息。此命令没有 -File 选项。
我懂了。您必须创建一个包含命令 powershell -executionpolicy bypass -File C:\Users\david\Desktop\test.ps1 的快捷方式。即使已给出安全全局命令 Set-ExecutionPolicy Restricted,也会运行命令 test.ps1。我希望这些基本信息对某人有所帮助。
h
hackjutsu
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

即使发生以下错误,上述命令也对我有用:

Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.

d
danday74

另外值得一提的是,您可能需要在脚本名称前包含 .\。例如:

.\scriptname.ps1

B
Brian Mains

命令 set-executionpolicy unrestricted 将允许您创建的任何脚本以登录用户身份运行。只需确保在注销之前使用 set-executionpolicy signed 命令将执行策略设置回签名。


set-executionpolicy signed 给出 Cannot bind parameter 'ExecutionPolicy' 等。
W
Wasif

我们可以很好地绕过执行策略(在命令提示符内):

type file.ps1 | powershell -command -

或者在powershell里面:

gc file.ps1|powershell -c -

M
MaartenDev

在 Windows 10 上:单击更改 myfile.ps1 的安全属性并通过右键单击 myfile.ps1 上的 / 属性更改“允许访问”


C
Christian Brüggemann

绕过执行策略是理想的,例如通过

powershell -executionpolicy bypass -File .\MYSCRIPT.ps1

不幸的是,这仍然可以通过组策略来防止。作为一种解决方法,您可以通过在 PowerShell 中运行将脚本编码为 Base64:

[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes((Get-Content .\MYSCRIPT.ps1)))

然后像这样执行结果:

powershell.exe -EncodedCommand "put-your-base64-string-here"

警告:这不适用于需要参数的脚本。