我正在尝试运行从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 ISE编辑器中,我发现运行以下第一行允许的脚本。

Set-ExecutionPolicy RemoteSigned -Scope Process

其他回答

如果您使用的是Windows Server 2008 R2,则存在x64和x86版本的PowerShell,这两个版本都必须设置执行策略。您是否在两台主机上都设置了执行策略?

作为管理员,您可以通过在PowerShell窗口中键入以下内容来设置执行策略:

Set-ExecutionPolicy RemoteSigned

有关详细信息,请参阅使用Set ExecutionPolicy Cmdlet。

完成后,可以使用以下方法将策略设置回其默认值:

Set-ExecutionPolicy Restricted

您可能会看到一个错误:

Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. 
To change the execution policy for the default (LocalMachine) scope, 
  start Windows PowerShell with the "Run as administrator" option. 
To change the execution policy for the current user, 
  run "Set-ExecutionPolicy -Scope CurrentUser".

因此,您可能需要运行如下命令(如注释所示):

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

对于Windows 11。。。

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

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

运行Set ExecutionPolicy RemoteSigned命令

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

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

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

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

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

以管理员身份打开PowerShell并运行Set ExecutionPolicy-Scope CurrentUser提供RemoteSigned并按Enter键运行集执行策略-作用域CurrentUser提供无限制并按Enter键