PowerShell说;在此系统上禁用脚本的执行。”;

PowerShell说;在此系统上禁用脚本的执行。”;,powershell,windows-server-2008-r2,Powershell,Windows Server 2008 R2,我正在尝试运行一个cmd文件,该文件从cmd.exe调用PowerShell脚本,但出现以下错误: 无法加载Management\u Install.ps1,因为在此系统上禁用了脚本的执行 我运行了以下命令: Set-ExecutionPolicy -ExecutionPolicy Unrestricted 当我从PowerShell运行Get ExecutionPolicy时,它返回Unrestricted PS C:\Users\Administrator\> Get-Executi

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

无法加载
Management\u Install.ps1
,因为在此系统上禁用了脚本的执行

我运行了以下命令:

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\u Install.ps1

警告:正在运行x86 PowerShell

无法加载文件
C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts\Management\u Install.ps1
,因为在此系统上禁用了脚本的执行。有关详细信息,请参阅“
获取有关签名的帮助”

第1行字符:25

  • \Management\u Install.ps1
    如果您使用的是R2,则有一个x64和x86版本的PowerShell,这两个版本都必须设置其执行策略。您是否在两台主机上都设置了执行策略

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

    Set-ExecutionPolicy RemoteSigned
    
    有关详细信息,请参阅

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

    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".
    
    因此,您可能需要像这样运行命令(如注释中所示):


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

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

    Set-ExecutionPolicy RemoteSigned
    

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

    powershell -ExecutionPolicy Bypass -File script.ps1
    

    在脚本之前运行此命令也可以解决此问题:

    set-executionpolicy unrestricted
    

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

    在Windows 7中:

    转到“开始”菜单并搜索“Windows PowerShell ISE”

    右键单击x86版本并选择“以管理员身份运行”


    在顶部,粘贴
    设置ExecutionPolicy RemoteSigned
    ;运行脚本。选择“是”

    对64位版本的Powershell ISE(非x86版本)也重复这些步骤


    我只是想澄清一下@Chad Miller暗示的步骤。谢谢你,查德

    我也有类似的问题,并注意到默认的
    cmd
    on正在运行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\u ARCHITECTURE%
    • 在Powershell中:
      [Environment]::is64位进程
    参考文献:


    如果您所在的环境不是管理员,您可以只为自己设置执行策略,而不需要管理员

    Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
    

    您可以在帮助条目中阅读有关它的所有信息

    Help Get-ExecutionPolicy -Full
    Help Set-ExecutionPolicy -Full
    

    如果您在这里是因为使用或运行它并使用``系统执行,请按如下方式执行:

    `powershell.exe -ExecutionPolicy Unrestricted -command [Environment]::GetFolderPath(\'mydocuments\')`
    
    该命令用于获取“MyDocuments”文件夹

    -ExecutionPolicy Unrestricted
    起作用


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

    现有的大多数答案都解释了如何解决问题,但很少有人解释原因。在你执行来自互联网上陌生人的代码之前,尤其是禁用安全措施的代码,你应该准确地了解你在做什么。下面是关于这个问题的更多细节

    从TechNet:

    Windows PowerShell执行策略允许您确定Windows PowerShell加载配置文件和运行脚本的条件

    如所列举的,其好处包括:

    • 执行控制-控制执行脚本的信任级别
    • 命令Highjack-防止在我的路径中插入命令
    • 标识是由我信任的开发人员创建和签署的脚本,和/或由我信任的证书颁发机构的证书签署的脚本
    • 完整性-恶意软件或恶意用户无法修改脚本
    要检查当前执行策略,可以运行。但你来这里可能是因为你想改变它

    为此,您将运行cmdlet

    在更新执行策略时,您需要做出两个主要决策

    执行策略类型:
    • 受限
      †-无法在系统上执行本地、远程或下载的脚本
    • AllSigned
      -所有运行的脚本都需要进行数字签名
    • 远程签名
      -所有远程脚本(UNC)或下载的脚本都需要签名
    • 无限制
      -任何类型的脚本都不需要签名
    新变化的范围
    • LocalMachine
      †-执行策略影响计算机的所有用户
    • 当前用户
      -执行策略仅影响当前用户
    • 过程
      -
      Help Get-ExecutionPolicy -Full
      Help Set-ExecutionPolicy -Full
      
      `powershell.exe -ExecutionPolicy Unrestricted -command [Environment]::GetFolderPath(\'mydocuments\')`
      
      Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
      
      %UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
      
      $profile
      
      Set-ExecutionPolicy RemoteSigned -Scope Process
      
      Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force -Scope Process
      
          runas.exe /savecred /user:administrator powershell -ExecutionPolicy ByPass -File script.ps1
      
      Get-ExecutionPolicy;
      
      Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;
      
      Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force;
      
      PS > powershell Get-Content .\test.ps1 | Invoke-Expression
      
      powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
      
      powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "AllSigned"
      
      Invoke-Command -ComputerName $servername -ConfigurationName Microsoft.PowerShell32 -scriptblock {Set-ExecutionPolicy unrestricted}
      
      Get-Content "PS1scriptfullpath.ps1" | Powershell-NoProfile -
      
      Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
      
      Set-ExecutionPolicy -ExecutionPolicy Remotesigned 
      
      Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
      
      C:\Users\%username%\AppData\Roaming\npm\
      
      Set-ExecutionPolicy RemoteSigned