首页 文章

PowerShell说“在这个系统上禁用了脚本的执行 . ”

提问于
浏览
1354

我正在尝试运行从命令提示符调用PowerShell脚本的.cmd文件,我收到以下错误:

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

我已经运行了 set-executionpolicy unrestricted ,当我从PowerShell运行 get-executionpolicy 时,我得到了 unrestricted .

//从Powershell输出PS C:\ Users \ Administrator> get-executionpolicy Unrestricted //从DOS C输出:\ Projects \ Microsoft.Practices.ESB \ Source \ Samples \ Management Portal \ Install \ Scr ipts> powershell . \ Management_Install .ps1 1警告:运行x86 PowerShell ...无法加载文件C:\ Projects \ Microsoft.Practices.ESB \ Source \ Samples \ Management Portal \ Install \ Scripts \ Management_Install.ps1,因为在此系统上禁用了脚本的执行 . 有关详细信息,请参阅“get-help about_signing” . 在行:1 char:25 . \ Management_Install.ps1 <<<< 1 CategoryInfo:NotSpecified:(:) [],PSSecurityException FullyQualifiedErrorId:RuntimeException C:\ Projects \ Microsoft.Practices.ESB \ Source \ Samples \ Management Portal \ Install \脚本>暂停按任意键继续 . . .

该系统是Windows Server 2008 R2 .

我究竟做错了什么?

22 回答

  • 13

    Win R并键入复制粘贴命令,然后按确定:

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

    并执行您的脚本 .

    然后还原更改,如:

    powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "AllSigned"
    
  • 27

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

  • 23

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

    set-executionpolicy unrestricted
    
  • 17

    在PowerShell 2.0中,默认情况下将执行策略设置为禁用 .

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

    在您的情况下,从PowerShell控制台键入 Set-ExecutionPolicy RemoteSigned 并说“是” .

  • 0

    转到注册表路径 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell 并将 ExecutionPolicy 设置为 RemoteSigned .

  • 99

    如果您正在使用Windows Server 2008 R2,那么有一个x64和x86版本的PowerShell,这两个版本都必须设置其执行策略 . 您是否在两台主机上设置了执行策略?

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

    Set-ExecutionPolicy RemoteSigned
    

    有关更多信息,请参阅Using the Set-ExecutionPolicy Cmdlet .

  • 4

    如果您所在的环境中不是管理员,则可以仅为您设置执行策略,并且不需要管理员 .

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

    要么

    Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted"
    

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

    Help Get-ExecutionPolicy -Full
    Help Set-ExecutionPolicy -Full
    
  • 34

    在Windows 7中:

    转到“开始”菜单,然后搜索“Windows PowerShell ISE” .

    右键单击x86版本,然后选择“以管理员身份运行” .

    在顶部,粘贴 Set-ExecutionPolicy RemoteSigned ;运行脚本 . 选择"Yes" .

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

    我只是澄清了@Chad Miller所暗示的步骤 . 谢谢乍得!

  • 576

    大多数现有的答案解释了How,但很少解释为什么 . 在你开始执行互联网上陌生人的代码之前,特别是禁用安全措施的代码之前,你应该准确理解你对这个问题的更多细节 .

    来自TechNet About Execution Policies Page

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

    PowerShell Basics - Execution Policy and Code Signing所列举的好处是:

    执行控制 - 控制执行脚本的信任级别 . 命令Highjack - 防止在我的路径中注入命令 . 身份 - 脚本是由我信任的开发人员创建和签名的,和/或使用我信任的证书颁发机构的证书签名 . 完整性 - 恶意软件或恶意用户无法修改脚本 .

    要检查当前的执行策略,可以运行Get-ExecutionPolicy . 但你可能在这里是因为你想改变它 .

    为此,您将运行Set-ExecutionPolicy cmdlet .

    在更新执行策略时,您将做出两个重大决策 .

    执行政策类型:

    • Restricted † - 无法在系统上执行本地,远程或下载的脚本 .

    • AllSigned - 所有运行的脚本都需要进行数字签名 .

    • RemoteSigned - 需要签署所有远程脚本(UNC)或下载的脚本 .

    • Unrestricted - 不需要任何类型脚本的签名 .

    新变化的范围

    • LocalMachine † - 执行策略会影响计算机的所有用户 .

    • CurrentUser - 执行策略仅影响当前用户 .

    • Process - 执行策略仅影响当前的Windows PowerShell进程 .

    †=默认

    例如:如果要将策略更改为仅用于CurrentUser的RemoteSigned,则运行以下命令:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
    

    Note :要更改执行策略,您必须运行 PowerShell As Adminstrator . 如果您处于常规模式并尝试更改执行策略,则会收到以下错误:

    拒绝访问注册表项“HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ PowerShell \ 1 \ ShellIds \ Microsoft.PowerShell” . 若要更改默认(LocalMachine)范围的执行策略,请使用“以管理员身份运行”选项启动Windows PowerShell .

    如果你想收紧你自己没有从互联网上下载的脚本的内部限制(或至少在Signing PowerShell Scripts上发表文章) .

    Note :大多数人在打开Powershell时都可能会收到此错误,因为PS在启动时尝试做的第一件事就是执行设置环境的用户配置文件脚本但是你喜欢它 .

    该文件通常位于:

    %UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
    

    您可以通过运行powershell变量找到确切的位置

    $profile
    

    如果在配置文件中没有您关心的任何内容,并且不想对您的安全设置大惊小怪,您可以删除它并且powershell将找不到任何无法执行的内容 .

  • 35

    在PowerShell ISE编辑器中,我发现运行以下行首先允许的脚本 .

    Set-ExecutionPolicy RemoteSigned -Scope Process
    
  • 9

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

    对于 Windows 7Windows 8Windows Server 2008 R2Windows Server 2012 ,运行以下命令为 Administrator

    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

    References:
    MSDN - Windows PowerShell execution policies
    Windows - 32bit vs 64bit directory explanation

  • 7

    我发现这条线最适合我的一个Windows Server 2008 R2服务器 . 在我的PowerShell脚本中,如果没有这一行,其他几个没有问题:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force -Scope Process
    
  • 3

    您可以通过在运行PowerShell时添加 -ExecutionPolicy ByPass 来绕过此策略

    powershell -ExecutionPolicy ByPass -File script.ps1
    
  • 2
    • 以管理员身份打开PowerShell并运行 Set-ExecutionPolicy -Scope CurrentUser

    • 提供 RemoteSigned 并按Enter键

    • 运行 Set-ExecutionPolicy -Scope CurrentUser

    • 提供 Unrestricted 并按Enter键

  • 1

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

    Get-ExecutionPolicy;
    

    默认情况下,它是 Restricted . 为了允许执行PowerShell脚本,我们需要将此ExecutionPolicy设置为 BypassUnrestricted .

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

    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;
    
    Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force;
    

    Unrestricted policy加载所有配置文件并运行所有脚本 . 如果运行从Internet下载的未签名脚本,则会在运行前提示您获得权限 .

    而在 Bypass 策略中,没有任何内容被阻止,并且在脚本执行期间没有警告或提示 . 绕过 ExecutionPolicyUnrestricted 更放松 .

  • 1

    我正在使用 Windows 10 并且无法运行任何命令 . 给我一些线索的唯一命令就是:

    [64]

    打开C:\ Windows \ SysWOW64 \ cmd.exe [以管理员身份]运行命令> powershell Set-ExecutionPolicy Unrestricted

    但这没效果 . 它有限 . 可能是Windows10的新安全策略 . 我有这个错误:

    Set-ExecutionPolicy:Windows PowerShell已成功更新执行策略,但该设置被更具体范围内定义的策略覆盖 . 由于覆盖,您的shell将保留其当前有效的执行策略...

    所以我发现了另一种方式( solution ):

    • Open Run Command / Console(Win R)

    • 类型: gpedit.mscGroup Policy编辑)

    • 浏览到本地计算机策略 - >计算机配置 - >管理模板 - > Windows组件 - > Windows Powershell .

    • 启用“ Turn on Script Execution

    • 根据需要设置策略 . 我将我的设置为“ Allow all scripts ” .

    现在打开PowerShell并享受;)

  • 0

    如果你因为使用RubyChef并使用``系统执行来运行它,请执行如下操作:

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

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

    -ExecutionPolicy Unrestricted 诀窍 .

    我希望它对其他人有帮助 .

  • 0

    我今天遇到了同样的问题 . 64位执行策略不受限制,而32位受限制 .

    以下是如何远程更改32位策略:

    Invoke-Command -ComputerName $servername -ConfigurationName Microsoft.PowerShell32 -scriptblock {Set-ExecutionPolicy unrestricted}
    
  • 0

    您还可以使用以下命令绕过此操作:

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

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

    15 Ways to Bypass the PowerShell Execution Policy

  • 0

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

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

    Set-ExecutionPolicy RemoteSigned
    
  • 112

    几个答案指向执行政策 . 但有些事情也需要“runas管理员” . 这是最安全的,因为执行政策没有永久性的变化,并且可以通过管理员限制 . 使用schedtask启动批处理:

    runas.exe /savecred /user:administrator powershell -ExecutionPolicy ByPass -File script.ps1
    

    来自上面的杰克埃德蒙兹和后期的Peter Mortensen / Dhana How to run an application as "run as administrator" from the command prompt?

  • 1776

    Administrator 打开PowerShell窗口 . 它会工作 .

相关问题