I'm having a problem with sending a JSON variable from PowerShell to python.
That's the code I have for python:
import subprocess
import os
import json
firstname = 'FirstName'
lastname = 'LastName'
args2 = '-FIRSTNAME %s -LASTNAME %s' % (firstname, lastname)
test = subprocess.run([
'powershell.exe',
'Path\\Test2.ps1',
args2,
])
jsonvalue = json.loads(test)
print(jsonvalue)
And here's for PowerShell:
Param(
[Parameter(Mandatory)][string]$FIRSTNAME,
[Parameter(Mandatory)][string]$LASTNAME
)
$result = [PSCustomObject]@{
FirstName = $FIRSTNAME
LastName = $LASTNAME
FullName = $FIRSTNAME + " " + $LASTNAME
}
return $result | ConvertTo-Json
JSON output:
{
"FirstName": "FirstName",
"LastName": "LastName",
"FullName": "FirstName LastName"
}
The error message I'm getting is:
TypeError: the JSON object must be str, bytes or bytearray, not CompletedProcess
From what I've read it looks like subprocess.run is returning CompletedProcess variable type. Unfortunately, although I tried to bypass it wouldn't work.
I think it might be pretty simple, but I cannot find an answer on my own. I'm pretty new to Python and connecting those two languages might be a bit of overkill for a beginner but isn't that the best way to learn? :D
-FIRSTNAME,firstname, etc all be separate elements ofrun's list argument, rather than combined in a single string value?arg2 = ['-FIRSTNAME', firstname, '-LASTNAME', lastname]; run(['powershell.exe', 'PATH...', *args2])?