0

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@email.com", $password)
    $ses = New-PSSession -Name "email@email.com" -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)
  • What are you trying to do with the data? `Get-User` is returning a PS object for the user. What information are you trying to get from Exchange online? [here's some of the information on the object return from `get-user`](https://learn.microsoft.com/en-us/powershell/module/exchange/get-user?view=exchange-ps) – another victim of the mouse Apr 26 '22 at 22:34
  • You CAN pipe `get-user` into `ConvertTo-Json` which might be a bit easier to work with in python. – another victim of the mouse Apr 26 '22 at 22:42

2 Answers2

0

I figured out the solution own my own. 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@email.com", $password)
    $ses = New-PSSession -Name "email@email.com" -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)

EDIT - It seems the stackoverflow user another victim of the mouse just copied my answer to make his own. You can tell by the timing of the comment and his post that he just copied my answer after I already found the answer for myself.

0

Here's reusable function:

function Get-JsonEmailList { 
    [CmdletBinding()]
    param (
        [Parameter()]
        [String]
        $target
    )
    $email = 'email@email.com'
    $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