我正在尝试运行从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。

我做错了什么?


当前回答

您可以使用特殊方式绕过它:

Get-Content "PS1scriptfullpath.ps1" | Powershell -NoProfile -

它将PowerShell脚本的内容通过管道传输到PowerShell.exe,并绕过执行策略执行它。

其他回答

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

您可以将其设置为RemoteSigned:

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

或不受限制:

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

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

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

在我的情况下,发生这种情况是因为我使用PowerShell,我应该使用cmd提示符

打开Windows PowerShell命令窗口并运行以下查询以更改ExecutionPolicy:

设置ExecutionPolicy-ExecutionPolicy RemoteSigned-作用域CurrentUser

如果要求确认更改,请按Y键并按Enter键。

我发现这一行最适合我的一台Windows Server 2008 R2服务器。其他一些人在我的PowerShell脚本中没有这行代码时没有问题:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force -Scope Process

我也面临过类似的问题。试试这个。

在使用Windows时,我遵循了以下步骤。以管理员身份打开命令提示符,然后转到以下路径:

C:\Users\%username%\AppData\Roaming\npm\

在此文件夹(目录)中查找文件ng.ps1然后删除它(delng.ps1)。

您也可以在这之后清除npm缓存,尽管它也可以在不执行此步骤的情况下工作。