为什么我的PowerShell脚本无法运行?

115
我将为您翻译以下内容,涉及IT技术:

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

它在我的路径中的一个脚本目录中。 这是我得到的错误:

无法加载,因为在此系统上禁用了脚本执行。 请参阅“get-help about-signing”。

我查看了帮助,但一无所获。

11个回答

114

可能是 PowerShell 的默认安全级别,据我所知,它只会运行已签名的脚本。

尝试输入以下内容:

set-executionpolicy remotesigned

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

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


5
你需要以管理员权限运行 PowerShell,至少在 Windows 8 下! - ComFreek
1
你必须在Windows 7下以管理员权限运行PowerShell脚本。 - Rod
21
这是一个相当可怕的答案。首先,它以可能不希望(且不安全的)方式永久性地更改了PowerShell的默认安全级别。其次,它甚至未能充分解释已授予执行权限的签名远程脚本和未签名本地脚本 - 但未签名远程脚本则不会如此,而Chocolatey偶尔需要这样做。大多数用户可能希望使用方法。 - Cecil Curry
3
虽然Cecil对答案中的一些弱点的担忧是合理的,但我想指出几件事情。1)他的两个链接对我来说似乎打开了同一个页面。2)如果你想让脚本简单地运行,设置执行策略可能仍然是最好的方法,与OP可能无关:3)似乎最好的策略是对于启动脚本,在运行脚本的命令行中指定执行策略和范围,在登录时根据需要设置会话配置。如果你想在Windows会话期间保持高安全性但在登录时降低安全性,你需要在脚本顺序上变得细致。 - omJohn8372
对于一次性的操作,我发现下面Ankur的回答最有帮助,它为正在运行的PowerShell实例定义了一个临时异常。 - Florenz Kley
显示剩余2条评论

90

你需要运行 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.

4
Windows安装完成后,默认的执行策略(ExecutionPolicy)取值是什么? - Matthew Lock
6
“Restricted”是默认策略。了解更多 - Nadeem_MK

33

使用:

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

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


1
这个 + https://superuser.com/questions/612409/how-do-i-run-multiple-commands-on-one-line-in-powershell + 快捷方式链接 = 完美 - Monday Fatigue

23
我通过这种方式调用PowerShell成功绕过了这个错误:
powershell -executionpolicy bypass -File .\MYSCRIPT.ps1

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

这在Windows 7 Service Pack 1上运行良好。由于我对PowerShell还不熟悉,可能存在我不知道的注意事项。

[编辑2017-06-26] 我已经继续在其他系统上使用这种技术,包括Windows 10和Windows 2012 R2,没有出现任何问题。

这是我现在正在使用的方式。这样可以避免我通过单击来意外运行脚本。当我在计划程序中运行它时,我会添加一个参数:“scheduler”,并且这将绕过提示。

这也会在最后暂停窗口,以便我可以看到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的方式。 - icc97
我尝试运行命令Set-ExecutionPolicy bypass -File C:\Users\david\Desktop\test.ps1,但是出现了错误信息。该命令没有-File选项。 - David Spector
哦,我明白了。你需要创建一个快捷方式,其中包含命令powershell -executionpolicy bypass -File C:\Users\david\Desktop\test.ps1。即使已经给出了安全的全局命令Set-ExecutionPolicy Restricted,也会运行命令test.ps1。希望这些基本信息能帮助到某些人。 - David Spector
我的理解是否正确,-File标志表示执行策略仅适用于文件“MYSCRIPT.ps1”,并防止绕过执行策略在全局范围内设置?谢谢! - Drew
@Drew 这是我对它工作方式的理解,但我不是PS专家。我只是不明白它如何能够全局应用。 - Be Kind To New Users
我也是!我会假设我们的假设都是正确的 :) 谢谢! - Drew

6
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

上述命令即使在出现以下错误时也能正常工作:
Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.

你应该以管理员身份运行PowerShell执行该命令。 - Mohammed Khurram

5
此外,值得注意的是,在脚本名称前面可能需要包含 .\。例如:
.\scriptname.ps1

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

"set-executionpolicy signed" 给出了 "无法绑定参数 'ExecutionPolicy'" 等信息。 - JinSnow

1
我们可以以一种好的方式绕过执行策略(在命令提示符中):
type file.ps1 | powershell -command -

或者在 PowerShell 中:

gc file.ps1|powershell -c -

0
这个问题发生在我身上,是因为我一直在使用node版本管理工具nvm,它错误地复制了node版本目录。然后我开始手动复制它们,这样做没问题,但是我有一个旧版本的node,其中安装了一个不完整的npm版本。node版本文件夹缺少npm.cmd文件。当我将这个文件添加到node目录中后,我就能够再次从bash终端运行'powershell myexmple.ps1',并且出于某种原因,我也能够再次在powershell中运行powershell脚本...真是奇怪。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接