且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PowerShell 说“在此系统上禁止执行脚本".

更新时间:2023-12-04 19:18:04

如果您使用的是 Windows Server 2008 R2 然后有 x64x86 版本的 PowerShell,这两个版本都必须设置执行策略.您是否在两台主机上都设置了执行策略?

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

Set-ExecutionPolicy RemoteSigned

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

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

Set-ExecutionPolicy 受限

您可能会看到一个错误:

访问注册表项'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' 被拒绝.要更改默认 (LocalMachine) 范围的执行策略,使用以管理员身份运行"启动 Windows PowerShell选项.要更改当前用户的执行策略,运行Set-ExecutionPolicy -Scope CurrentUser".

所以你可能需要像这样运行命令(如评论中所示):

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

I am trying to run a cmd file that calls a PowerShell script from cmd.exe, but I am getting this error:

Management_Install.ps1 cannot be loaded because the execution of scripts is disabled on this system.

I ran this command:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

When I run Get-ExecutionPolicy from PowerShell, it returns Unrestricted.

PS C:\Users\Administrator\> Get-ExecutionPolicy
Unrestricted


C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts> powershell .\Management_Install.ps1 1

WARNING: Running x86 PowerShell...

File C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts\Management_Install.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.

At line:1 char:25

  • .\Management_Install.ps1 <<<< 1

    • CategoryInfo : NotSpecified: (:) [], PSSecurityException

    • FullyQualifiedErrorId : RuntimeException

C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts> PAUSE

Press any key to continue . . .


The system is Windows Server 2008R2.

What am I doing wrong?

If you're using Windows Server 2008 R2 then there is an x64 and x86 version of PowerShell both of which have to have their execution policies set. Did you set the execution policy on both hosts?

As an Administrator, you can set the execution policy by typing this into your PowerShell window:

Set-ExecutionPolicy RemoteSigned

For more information, see Using the Set-ExecutionPolicy Cmdlet.

When you are done, you can set the policy back to its default value with:

Set-ExecutionPolicy Restricted

You may see an error:

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".

So you may need to run the command like this (as seen in comments):

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser