我正在尝试运行从cmd.exe调用PowerShell脚本的cmd文件,但遇到以下错误:

无法加载Management_Install.ps1,因为在此系统上禁用了脚本的执行。

我运行了以下命令:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

当我从PowerShell运行Get-ExecutionPolicy时,它返回Unrestricted。

Get-ExecutionPolicy

输出:

Unrestricted

cd“C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts”powershell。\管理安装.ps1 1警告:正在运行x86 PowerShell。。。无法加载文件C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts\Management_Install.ps1,因为在此系统上禁用了脚本的执行。有关详细信息,请参阅“get-helpabout_signing”。第1行字符:25.\Management_Install.ps1<<<1类别信息:未指定:(:)[],PSSecurityExceptionFullyQualifiedErrorId:运行时异常C: \Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts>PAUSE按任意键继续。


系统为Windows Server 2008 R2。

我做错了什么?


当前回答

您应该运行以下命令:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

其他回答

在运行PowerShell时,可以通过添加-ExecutionPolicy bypass来绕过单个文件的此策略

powershell -ExecutionPolicy Bypass -File script.ps1

转到注册表路径HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shell\1\ShellIds\Microsoft.PowerShell,并将ExecutionPolicy设置为RemoteSigned。

删除C:\Users[your user]\AppData\Roaming\npm中的ng.ps1文件。

然后删除C:\Users\USER\AppData\Roaming中的npm缓存文件。

要解决此问题,我们必须设置执行策略,以便PowerShell脚本在特定计算机上运行。以下是方法:

通过选择“以管理员身份运行”打开PowerShell控制台,并使用以下命令设置执行策略:set ExecutionPolicy RemoteSigned当提示继续时,键入“Y”

信用:https://www.sharepointdiary.com/2014/03/fix-for-powershell-script-cannot-be-loaded-because-running-scripts-is-disabled-on-this-system.html

如果您所处的环境不是管理员,则可以仅为您(当前用户)设置执行策略,而不需要管理员。

您可以将其设置为RemoteSigned:

Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"

或不受限制:

Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted"

您可以在帮助条目中阅读有关获取和设置执行策略的所有信息:

Help Get-ExecutionPolicy -Full
Help Set-ExecutionPolicy -Full