Skip to content

[Regression] Can't import Security module when calling PowerShell from Python #18681

Closed

Description

Prerequisites

Steps to reproduce

I have a python script that runs a .ps1 file which includes commands from the Microsoft.PowerShell.Security module. Starting with 7.3.0, these commands fail to import during script execution. This can be reproduced from a PowerShell window:

python -c "import subprocess; subprocess.Popen('powershell -command Get-ExecutionPolicy')"

OR

python -c "import subprocess; subprocess.Popen('powershell -command Import-Module Microsoft.PowerShell.Security')"

Expected behavior

PS C:\> python -c "import subprocess; subprocess.Popen('powershell -command Get-ExecutionPolicy')"
PS C:\> Restricted
PS C:\> python -c "import subprocess; subprocess.Popen('powershell -command Import-Module Microsoft.PowerShell.Security')"
PS C:\>

Actual behavior

PS C:\> python -c "import subprocess; subprocess.Popen('powershell -command Get-ExecutionPolicy')"
PS C:\> Get-ExecutionPolicy : The 'Get-ExecutionPolicy' command was found in the module 'Microsoft.PowerShell.Security', but the module could not be loaded. For more
information, run 'Import-Module Microsoft.PowerShell.Security'.
At line:1 char:1
+ Get-ExecutionPolicy
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-ExecutionPolicy:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CouldNotAutoloadMatchingModule


PS C:\> python -c "import subprocess; subprocess.Popen('powershell -command Import-Module Microsoft.PowerShell.Security')"
PS C:\> Import-Module : The following error occurred while loading the extended type data file: Error in TypeData "System.Security.AccessControl.ObjectSecurity": The
member AuditToString is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member AccessToString is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Sddl is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Access is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Group is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Owner is already present.
Error in TypeData "System.Security.AccessControl.ObjectSecurity": The member Path is already present.
At line:1 char:1
+ Import-Module Microsoft.PowerShell.Security
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Import-Module], RuntimeException
    + FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand

Error details

No response

Environment data

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.0
PSEdition                      Core
GitCommitId                    7.3.0
OS                             Microsoft Windows 10.0.22622
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS> python --version
Python 3.10.6

Visuals

No response

Activity

  1. added
    Needs-Triage
    on Nov 30, 2022
  2. zurcher commented on Nov 30, 2022

    @zurcher
    Author

    Following this comment from the PowerShell VSCode extension chat, I found that the behavior was resolved by commenting out the "TypesToProcess" line from Microsoft.PowerShell.Security.psd1

    The change was introduced in #16355

    This does not occur with PowerShell 7.2.7.

  3. jborean93 commented on Nov 30, 2022

    @jborean93
    Collaborator

    I believe this is a duplicate of #18530 but I'm not 100% sure. The problem is you start Python from PowerShell 7.3 which inherits the PSModulePath env var and that Python process then spawns Windows PowerShell (5.1) with that inherited env var. It will try to import Microsoft.PowerShell.Security that is provided by PowerShell 7.3.0 when it should be using what Windows PowerShell ships with.

  4. added
    Issue-Bug
    on Dec 5, 2022
  5. SteveL-MSFT commented on Dec 12, 2022

    @SteveL-MSFT
    Member

    Yes, this looks like a dupe of #18530, the workaround suggested here #18530 (comment) should work for you

  6. added
    Resolution-Duplicate
    and removed
    Needs-Triage
    on Dec 12, 2022
  7. zurcher commented on Dec 12, 2022

    @zurcher
    Author

    I'm hitting this bug in a long-term product support codebase and probably can't check in this workaround. I also suspect we'll see more reports of this issue as more people move to 7.3, and the error message gives no easy indication of what the problem is.

    If pwsh and powershell already know how to rebuild PSModulePath, could the pwsh -> powershell.exe special-case be applied to all executable calls?

  8. ghost closed this as completedon Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugResolution-Duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions