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

我做错了什么?


当前回答

在窗口10中:

如果您不是管理员,可以使用此选项:

powershell Set-ExecutionPolicy -Scope CurrentUser

cmdlet Set-ExecutionPolicy at command pipeline position 1
Supply values for the following parameters:
ExecutionPolicy: `RemoteSigned`

它像符咒一样解决了我的问题!

其他回答

我遇到了类似的问题,并注意到Windows Server 2012上的默认cmd运行的是x64版本。

对于Windows 11、Windows 10、Windows 7、Windows 8、Windows Server 2008 R2或Windows Server 2012,请以管理员身份运行以下命令:

x86(32位)打开C:\Windows\SysWOW64\cmd.exe运行命令powershell Set ExecutionPolicy RemoteSigned

x64(64位)打开C:\Windows\system32\cmd.exe运行命令powershell Set ExecutionPolicy RemoteSigned

您可以使用检查模式

在CMD中:echo%PROCESSOR_ARCHITECURE%在Powershell中:[Environment]::Is64BitProcess

参考文献:MSDN-Windows PowerShell执行策略Windows-32位vs 64位目录说明

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

如果您安装了Git,只需使用GitBash。

“无法加载,因为在此系统上禁用了运行脚本”的解决方案:如何启用此系统错误时禁用运行脚本?要解决此问题,我们必须设置执行策略,以便PowerShell脚本在特定计算机上运行。此处允许执行PowerShell脚本:

通过选择“以管理员身份运行”打开PowerShell控制台,并使用以下命令获取执行策略:GetExecutionPolicy以获取应用的当前策略,例如“受限”。使用以下命令设置执行策略:Set ExecutionPolicy RemoteSigned当提示继续时,键入“Y”。

首先,您需要打开PowerShell窗口并运行此命令。

set ExecutionPolicy RemoteSigned-作用域CurrentUser

然后它会要求您确认。键入Y并按Enter键。

运行此命令时,可以看到系统已将当前用户的所有策略设置为远程。完成此过程需要几秒钟。

图像如下所示:

检查执行策略是否已设置。类型:

获取执行策略

如果设置了它,输出将如下所示: