我正在尝试运行从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脚本在特定计算机上运行。以下是方法:

通过选择“以管理员身份运行”打开PowerShell控制台,并使用以下命令设置执行策略:set ExecutionPolicy RemoteSigned当提示继续时,键入“Y”

信用:https://www.sharepointdiary.com/2014/03/fix-for-powershell-script-cannot-be-loaded-because-running-scripts-is-disabled-on-this-system.html

其他回答

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

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

Set-ExecutionPolicy RemoteSigned

我们可以通过以下命令获取当前ExecutionPolicy的状态:

Get-ExecutionPolicy

默认情况下,它是受限的。为了允许执行PowerShell脚本,我们需要将此ExecutionPolicy设置为Unrestricted或Bypass。

我们可以使用以下任何PowerShell命令将当前用户的策略设置为绕过:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force

无限制策略加载所有配置文件并运行所有脚本。如果您运行的是从Internet下载的未签名脚本,则会在运行前提示您获得许可。

而在Bypass策略中,在脚本执行过程中不会阻止任何内容,也不会出现任何警告或提示。BypassExecutionPolicy比Unrestricted更宽松。

要解决此问题,我们必须设置执行策略,以便PowerShell脚本在特定计算机上运行。以下是方法:

通过选择“以管理员身份运行”打开PowerShell控制台,并使用以下命令设置执行策略:set ExecutionPolicy RemoteSigned当提示继续时,键入“Y”

信用:https://www.sharepointdiary.com/2014/03/fix-for-powershell-script-cannot-be-loaded-because-running-scripts-is-disabled-on-this-system.html

在我的情况下,发生这种情况是因为我使用PowerShell,我应该使用cmd提示符

设置执行策略是特定于环境的。如果您试图从正在运行的x86 ISE执行脚本,则必须使用x86 PowerShell设置执行策略。同样,如果您正在运行64位ISE,则必须使用64位PowerShell设置策略。