我正在尝试运行从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控制台,然后设置执行策略

Set-ExecutionPolicy -ExecutionPolicy Remotesigned 

其他回答

在PowerShell 2.0中,执行策略默认设置为禁用。

从那时起,PowerShell团队做了很多改进,他们相信用户在运行脚本时不会破坏很多东西。因此,从PowerShell 4.0开始,默认情况下会启用它。

在您的情况下,从PowerShell控制台键入Set-ExecutionPolicy RemoteSigned,然后说yes。

如果您在这里是因为使用Ruby或Chef运行它并使用“”系统执行,请执行如下操作:

`powershell.exe -ExecutionPolicy Unrestricted -command [Environment]::GetFolderPath(\'mydocuments\')`

该命令用于获取“MyDocuments”文件夹。

-ExecutionPolicy Unrestricted就是这样。

我希望这对其他人有帮助。

以管理员身份打开Powershell控制台,然后设置执行策略

Set-ExecutionPolicy -ExecutionPolicy Remotesigned 

打开Windows PowerShell命令窗口并运行以下查询以更改ExecutionPolicy:

设置ExecutionPolicy-ExecutionPolicy RemoteSigned-作用域CurrentUser

如果要求确认更改,请按Y键并按Enter键。

您也可以使用以下命令绕过此问题:

powershell Get-Content .\test.ps1 | Invoke-Expression

您还可以阅读Scott Sutherland的这篇文章,其中介绍了如果您没有管理员权限,绕过PowerShell Set ExecutionPolicy的15种不同方法:

绕过PowerShell执行策略的15种方法