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

我做错了什么?


当前回答

对于下载的文件,右键单击→ 财产

然后单击取消阻止并单击确定。然后运行PowerShell脚本,它将不再抱怨无法运行。

其他回答

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

powershell -ExecutionPolicy Bypass -File script.ps1

我今天也有同样的问题。64位执行策略不受限制,而32位执行策略受到限制。

以下是如何远程更改32位策略:

Invoke-Command -ComputerName $servername -ConfigurationName Microsoft.PowerShell32 -scriptblock {Set-ExecutionPolicy unrestricted}

对于Windows 11。。。

这确实很容易。只需打开设置应用程序。导航到隐私和安全:

单击For Developers并滚动到底部,找到PowerShell选项,在该选项下选中“Change the execution policy…remote scripts”(更改执行策略…远程脚本)复选框。

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

您可以将其设置为RemoteSigned:

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

或不受限制:

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

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

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

运行Set ExecutionPolicy RemoteSigned命令