[Python] How to get power shell output from python

Discussão em 'Python' iniciado por Stack, Maio 29, 2025.

  1. Stack

    Stack Membro Participativo

    If I use the following Python code to get the output of PowerShell it works fine:

    def get_powershell_table(command):
    try:
    result = subprocess.run(
    ["powershell", "-Command", command],
    capture_output=True,
    text=True,
    check=True,
    )
    return result.stdout
    except subprocess.CalledProcessError as e:
    print(f"Error executing PowerShell command: {e}")
    return None

    powershell_command = "Get-Process python | Format-Table -AutoSize"
    table_output = get_powershell_table(powershell_command)
    if table_output:
    print(table_output)


    This is consistent with the command I executed in the PowerShell terminal. However, when I execute:

    powershell_command = "Get-Process python | Format-Table CommandLine -AutoSize"


    there is no correct return. I don't know where the problem is?

    Continue reading...

Compartilhe esta Página