Powershell:无法读取/设置ExecutionPolicy

时间:2011-03-03 22:49:27

标签: powershell

我试图运行一个PowerShell脚本(我以前运行过)并收到以下错误:
授权管理员检查失败

我认为这是执行政策,所以跑了:

  

Set-ExecutionPolicy Unrestricted

然后我收到了:

  

Set-ExecutionPolicy:初始化失败
  在线:1字符:20
  + set-executionpolicy<<<<无限制
      + CategoryInfo:NotSpecified:(:) [Set-ExecutionPolicy],ManagementException
      + FullyQualifiedErrorId:System.Management.ManagementException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

Get-ExecutionPolicy

发生相同的初始化错误

一些基本命令似乎工作正常,例如:

  

写输出“hi”

有人可以建议修复吗?

注意:这是在Windows XP上

更新:如果我在powershell命令提示符下运行ascript的内容,它就可以运行。如果它作为脚本运行,我只会得到错误。此外,Get-ExecutionPolicy和Set-ExecutionPolicy仍然在powershell提示符中失败。

4 个答案:

答案 0 :(得分:2)

确保以管理员[右键单击]运行控制台,然后在运行Set-ExecutionPolicy Unrestricted时[单击]“以管理员身份运行”。

答案 1 :(得分:1)

错误的来源是由于WMI存储库出现问题。我能够修复运行以下命令的存储库:

注意:这适用于Windows XP

rundll32 wbemupgd, UpgradeRepository

运行后,我成功地再次执行Get-ExecutionPolicy和其他PowerShell脚本。

couple links详细修复WMI存储库。

答案 2 :(得分:0)

另一个非常常见的情况(我认为这仅适用于XP后),是从不受信任的位置下载后.ps1文件被“阻止”的地方。

解决方案:在Windows资源管理器中打开文件的“属性”,然后在“常规”选项卡上单击“取消阻止”,然后单击“应用”或“确定”。

答案 3 :(得分:0)

此错误的另一个潜在原因(在Windows Server 2012中可见)是Windows Management Instrumentation服务未运行。

启动并运行该服务允许运行Get-ExecutionPolicy,然后运行Set-ExecutionPolicy。