Home > Software engineering >  Parsing python variable retrieved from subprocess powershell function
Parsing python variable retrieved from subprocess powershell function

Time:04-27

I am trying to get a variable from powershell to python. I am testing a powershell function where I receive some data from a remote email server, then I am trying to pipe back the results with a return statement.

Python script:

import subprocess
import ast
data = subprocess.check_output(["pwsh", "/app/functions/intermedia_pwsh_functions.ps1","emaillist"])
print(data)

Powershell script:

function emaillist{
    $password = ConvertTo-SecureString "SOMELONGPASSWORD" -AsPlainText -Force
    $Cred = New-Object System.Management.Automation.PSCredential ("[email protected]", $password)
    $ses = New-PSSession -Name "[email protected]" -ConnectionUri https://exchange.intermedia.net/powershell -ConfigurationName Hosting.PowerShell -Credential $Cred -Authentication Basic
    Invoke-Command -Session $ses -ScriptBlock {Set-ConnectionSettings -CredentialType "User" -Credential $Using:Cred -AccountID 000000}
    $userdata = Invoke-Command -Session $ses -ScriptBlock {Get-User}
    return $userdata
}

I was able to pipe the output but it comes out very weird. enter image description here

I googled the error and got this link: https://stackoverflow.com/a/48214116 So I tried to convert this using ast but I kept getting an error when it tried to parse the weird data.

lst = ast.literal_eval(data.decode("ascii"))

but I got an error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 12202: ordinal not in range(128)

CodePudding user response:

I figured out a solution. Simply convert the data into json format and then you use json loads on the python side to parse the data.

powershell script:

function emaillist{
    $password = ConvertTo-SecureString "SOMELONGPASSWORD" -AsPlainText -Force
    $Cred = New-Object System.Management.Automation.PSCredential ("[email protected]", $password)
    $ses = New-PSSession -Name "[email protected]" -ConnectionUri https://exchange.intermedia.net/powershell -ConfigurationName Hosting.PowerShell -Credential $Cred -Authentication Basic
    Invoke-Command -Session $ses -ScriptBlock {Set-ConnectionSettings -CredentialType "User" -Credential $Using:Cred -AccountID 000000}
    $userdata = Invoke-Command -Session $ses -ScriptBlock {Get-User} | ConvertTo-Json
    return $userdata
}

Python script:

import subprocess
import json
data = subprocess.check_output(["pwsh", "/app/functions/intermedia_pwsh_functions.ps1","emaillist"])
data_loads = json.loads(data)
print(data_loads)

CodePudding user response:

Here's reusable function:

function Get-JsonEmailList { 
    [CmdletBinding()]
    param (
        [Parameter()]
        [String]
        $target
    )
    $email = '[email protected]'
    $password = ConvertTo-SecureString -String 'SOMELONGPASSWORD' -AsPlainText -Force
    $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($email, $password)
    $ses = New-PSSession -Name $email -ConnectionUri https://exchange.intermedia.net/powershell -ConfigurationName Hosting.PowerShell -Credential $Cred -Authentication Basic
    Invoke-Command -Session $ses -ScriptBlock { Set-ConnectionSettings -CredentialType 'User' -Credential $Using:Cred -AccountID 000000 }
    $userdata = Invoke-Command -Session $ses -ScriptBlock { Get-User $target | ConvertTo-Json } 
    return $userdata
}

get-JsonEmailList 
  • Related