【问题标题】:PowerShell on Windows 7: Set-ExecutionPolicy for regular usersWindows 7 上的 PowerShell:普通用户的 Set-ExecutionPolicy
【发布时间】:2011-06-06 13:18:16
【问题描述】:

我想以普通用户身份在 Windows 7 上运行 PowerShell 脚本。每当我尝试时,我都会收到以下错误:

File C:\Users\danv\Documents\WindowsPowerShell\profile.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:2
+ . <<<<  'C:\Users\danv\Documents\WindowsPowerShell\profile.ps1'
    + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
    + FullyQualifiedErrorId : RuntimeException

尝试通过Set-ExecutionPolicy Unrestricted 解决失败:

PS C:\Users\danv> Set-ExecutionPolicy Unrestricted
Set-ExecutionPolicy : Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell'
is denied.
At line:1 char:20
+ Set-ExecutionPolicy <<<<  Unrestricted
    + CategoryInfo          : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

我可以以管理员身份运行Set-ExecutionPolicy Unrestricted 命令,但这似乎不会传播给非管理员用户。

如何以非管理员身份成功运行脚本?

【问题讨论】:

    标签: windows-7 powershell


    【解决方案1】:
    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
    

    这将为当前用户(存储在 HKEY_CURRENT_USER 中)而不是本地机器(HKEY_LOCAL_MACHINE)设置执行策略。如果您对计算机没有管理控制权,这很有用。

    RemoteSigned 是比Unrestricted 更安全的执行策略。如果您下载了一个脚本并且RemoteSigned 阻止您执行它,那么在审查脚本后,通过打开文件的属性并标记“取消阻止”来消除限制。如果这不可行,那么您可以将策略设置为 Unrestricted

    【讨论】:

    • 完美。这立即解决了我的问题。对于新用户来说,powershell 被宣传为默认不允许脚本的脚本语言,这有点令人沮丧。
    • “无限制”?请注意the security implications
    • 这个Set-ExecutionPolicy Unrestricted -Scope CurrentUser 命令解决了我的问题。❤
    【解决方案2】:

    如果您(或有帮助的管理员)以管理员身份运行 Set-ExecutionPolicy,则将为所有用户设置该政策。 (我建议使用“remoteSigned”而不是“无限制”作为安全措施。)

    注意:在 64 位操作系统上,您需要分别为 32 位和 64 位 PowerShell 运行 Set-ExecutionPolicy

    【讨论】:

    • 我已经尝试过了 - 将内容设置为 Unrestricted,因为管理员似乎对非管理员没有任何改变...
    • @DanVinton:然后发生了其他事情。建议再次设置为管理员,然后在管理员和普通 shell 中检查 Get-ExecutionPolicy。组策略可能会否决您的意愿。
    • 按照通常的 IT 方式,让某人(众所周知)看着你的肩膀解决了这个问题......谢谢!
    • 对于 64 位 Windows 8 的注释,您必须从提升的 PowerShell 窗口执行它,如下所示:Set-ExecutionPolicy RemoteSigned -Force,后跟 start-job { Set-ExecutionPolicy RemoteSigned -Force } -RunAs32。这是确保两个版本都得到解决的唯一方法。
    • 谢谢,@Darek!由于 ExecutionPolicy 每次都返回 AllSigned,我差点把头发扯掉。不是它终于坚持了,耶!
    【解决方案3】:

    这应该可以解决您的问题,您应该尝试运行以下命令:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser 
    

    【讨论】:

      【解决方案4】:
      Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
      

      通过 PowerShell 运行此命令。它对我有用,希望它也对你有用:)

      【讨论】:

        【解决方案5】:

        RemoteSigned 是比 Unrestricted 更安全的执行策略。

        set-executionpolicy remotesigned
        

        【讨论】:

          【解决方案6】:

          您可以使用这个 PowerShell 命令:

          Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
          

          【讨论】:

            【解决方案7】:
            Select ***Start > All Programs > Windows PowerShell version > Windows PowerShell***.
            Type ```Set-ExecutionPolicy RemoteSigned``` to set the policy to ```RemoteSigned```.
            Type ```Set-ExecutionPolicy Unrestricted``` to set the policy to ```Unrestricted```.
            Type ```Get-ExecutionPolicy``` to verify the current settings for the execution policy.
            Type ```Exit```.
            

            【讨论】:

              【解决方案8】:

              如果您是您电脑的管理员,您可以在您的电脑中输入以下命令

               Set-ExecutionPolicy Unrestricted
              

              powershell 窗口。您可能必须以管理员身份运行 shell。

              完成后,它会要求您确认, 如果要为所有全局用户设置无限制设置,请输入“A” 如果您只想为当前用户(管理员)设置不受限制的设置,请输入“Y”

              记住:- 您始终可以使用以下命令恢复所做的更改:

              Set-ExecutionPolicy Restricted
              

              【讨论】:

                猜你喜欢
                • 2023-03-20
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-11-30
                • 2021-08-16
                相关资源
                最近更新 更多