Why is my locally-created script not allowed to run under the RemoteSigned execution policy?

后端 未结 13 1004
自闭症患者
自闭症患者 2021-01-30 02:35

Since this question continues to attract responses that are either refuted by the question body or don\'t address the actual problem, please read th

相关标签:
13条回答
  • 2021-01-30 03:32

    This is an IDE issue. Change the setting in the PowerShell GUI. Go to the Tools tab and select Options, and then Debugging options. Then check the box Turn off requirement for scripts to be signed. Done.

    0 讨论(0)
  • 2021-01-30 03:33

    Is the file being blocked? I had the same issue and was able to resolve it by right clicking the .PS1 file, Properties and choosing Unblock.

    0 讨论(0)
  • 2021-01-30 03:34

    Some things to check:

    Can you change to unrestricted?

    Set-ExecutionPolicy Unrestricted
    

    Is the group policy set?

    • Computer Configuration\Administrative Templates\Windows Components\Windows PowerShell
    • User Configuration\Administrative Templates\Windows Components\Windows PowerShell

    Also, how are you calling Script.ps1?

    Does this allow it to run?

    powershell.exe -executionpolicy bypass -file .\Script.ps1
    
    0 讨论(0)
  • 2021-01-30 03:35

    I finally tracked this down to .NET Code Access Security. I have some internally-developed binary modules that are stored on and executed from a network share. To get .NET 2.0/PowerShell 2.0 to load them, I had added a URL rule to the Intranet code group to trust that directory:

    PS> & "$Env:SystemRoot\Microsoft.NET\Framework64\v2.0.50727\caspol.exe" -machine -listgroups
    Microsoft (R) .NET Framework CasPol 2.0.50727.5420
    Copyright (c) Microsoft Corporation.  All rights reserved.
    
    Security is ON
    Execution checking is ON
    Policy change prompt is ON
    
    Level = Machine
    
    Code Groups:
    
    1.  All code: Nothing
        1.1.  Zone - MyComputer: FullTrust
            1.1.1.  StrongName - ...: FullTrust
            1.1.2.  StrongName - ...: FullTrust
        1.2.  Zone - Intranet: LocalIntranet
            1.2.1.  All code: Same site Web
            1.2.2.  All code: Same directory FileIO - 'Read, PathDiscovery'
            1.2.3.  Url - file://Server/Share/Directory/WindowsPowerShell/Modules/*: FullTrust
        1.3.  Zone - Internet: Internet
            1.3.1.  All code: Same site Web
        1.4.  Zone - Untrusted: Nothing
        1.5.  Zone - Trusted: Internet
            1.5.1.  All code: Same site Web
    

    Note that, depending on which versions of .NET are installed and whether it's 32- or 64-bit Windows, caspol.exe can exist in the following locations, each with their own security configuration (security.config):

    • $Env:SystemRoot\Microsoft.NET\Framework\v2.0.50727\
    • $Env:SystemRoot\Microsoft.NET\Framework64\v2.0.50727\
    • $Env:SystemRoot\Microsoft.NET\Framework\v4.0.30319\
    • $Env:SystemRoot\Microsoft.NET\Framework64\v4.0.30319\

    After deleting group 1.2.3....

    PS> & "$Env:SystemRoot\Microsoft.NET\Framework64\v2.0.50727\caspol.exe" -machine -remgroup 1.2.3.
    Microsoft (R) .NET Framework CasPol 2.0.50727.9136
    Copyright (c) Microsoft Corporation.  All rights reserved.
    
    The operation you are performing will alter security policy.
    Are you sure you want to perform this operation? (yes/no)
    yes
    Removed code group from the Machine level.
    Success
    

    ...I am left with the default CAS configuration and local scripts now work again. It's been a while since I've tinkered with CAS, and I'm not sure why my rule would seem to interfere with those granting FullTrust to MyComputer, but since CAS is deprecated as of .NET 4.0 (on which PowerShell 3.0 is based), I guess it's a moot point now.

    0 讨论(0)
  • 2021-01-30 03:35

    What works for me was right-click on the .ps1 file and then properties. Click the "UNBLOCK" button. Works great fir me after spending hours trying to change the policies.

    0 讨论(0)
  • 2021-01-30 03:39

    When you run a .ps1 PowerShell script you might get the message saying “.ps1 is not digitally signed. The script will not execute on the system.” To fix it you have to run the command below to run Set-ExecutionPolicy and change the Execution Policy setting.

    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
    
    0 讨论(0)
提交回复
热议问题