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

后端 未结 13 1206
自闭症患者
自闭症患者 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: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.

提交回复
热议问题