Skip to content

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

@zurcher

Description

@zurcher

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-DuplicateThe issue is a duplicate.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions