PowerShell表示“在此系统上禁用脚本的执行。”

2024-04-29 03:16:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试运行一个cmd文件,该文件从cmd.exe调用PowerShell脚本,但出现以下错误:

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

我运行了以下命令:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

当我从PowerShell运行Get-ExecutionPolicy时,它返回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 . . .


系统是Windows Server 2008R2

我做错了什么


Tags: installcmdsourcescriptsmanagementmicrosoftportalprojects
3条回答

运行PowerShell时,可以通过添加-ExecutionPolicy Bypass绕过单个文件的此策略

powershell -ExecutionPolicy Bypass -File script.ps1

我也有类似的问题,并注意到Windows Server 2012上的默认cmd正在运行x64

对于Windows 10Windows 7Windows 8Windows Server 2008 R2Windows 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_ARCHITECTURE%
  • 在Powershell中:[Environment]::Is64BitProcess

参考文献:
MSDN - Windows PowerShell execution policies
Windows - 32bit vs 64bit directory explanation

如果您使用的是Windows Server 2008R2,那么PowerShell的x64x86版本都必须设置其执行策略。您是否在两台主机上都设置了执行策略

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

Set-ExecutionPolicy RemoteSigned

有关更多信息,请参见Using the 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

相关问题 更多 >