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

我做错了什么?


当前回答

我正在使用Windows 10,无法运行任何命令。唯一能给我一些线索的命令是:

〔x64〕

以管理员身份打开C:\Windows\SysWOW64\cmd.exe运行命令>powershell Set ExecutionPolicy Unrestricted

但这并不奏效。这是有限的。可能是Windows10的新安全策略。我出现了以下错误:

Set ExecutionPolicy:Windows PowerShell已成功更新您的执行策略,但该设置被在更特定范围内定义的策略覆盖。由于重写,您的shell将保留其当前有效的执行策略。。。

所以我找到了另一种方法(解决方案):

打开运行命令/控制台(Win+R)类型:gpedit.msc(组策略编辑器)浏览到本地计算机策略->计算机配置->管理模板->Windows组件->Windows Powershell。启用“启用脚本执行”根据需要设置策略。我将我的设置为“允许所有脚本”。

现在打开PowerShell并享受;)

其他回答

RemoteSigned:您自己创建的所有脚本都将运行,所有从Internet下载的脚本都需要由可信的发布者签名。

好的,只需键入以下内容即可更改策略:

Set-ExecutionPolicy RemoteSigned

如果您使用的是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

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

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

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

以“以管理员身份运行”的身份运行cmd或powershell

这也发生在我身上。对我来说,解决方案很简单。我没有意识到命令提示符中运行Nodemon的路径与安装包的位置不同。

所以它给了我你提到的同样的错误。

改变我的路径解决了这个问题。