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.
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)