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

转到“开始”菜单并搜索“Windows PowerShell ISE”。

右键单击x86版本并选择“以管理员身份运行”。

在顶部,粘贴SetExecutionPolicy RemoteSigned;运行脚本。选择“是”。

对64位版本的Powershell ISE(非x86版本)也重复这些步骤。

我只是在澄清Chad Miller暗示的步骤。谢谢Chad!

其他回答

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

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force -Scope Process

有几个答案指向执行策略。然而,有些事情也需要“runas管理员”。这是最安全的,因为执行策略没有永久性更改,并且可以通过管理员限制。与schedask一起使用以启动批处理:

    runas.exe /savecred /user:administrator powershell -ExecutionPolicy ByPass -File script.ps1

上面的Jack Edmonds和文章的Peter Mortensen/Dhana如何在命令提示符下以“以管理员身份运行”运行应用程序?

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

以管理员身份打开PowerShell。运行以下命令

设置ExecutionPolicy RemoteSigned

询问时键入Y!

在窗口10中:

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

powershell Set-ExecutionPolicy -Scope CurrentUser

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

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