Question:
Path example: C:\Users\user\Some Space\dev\sensor\
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def readSensorList(): with open('sensor.json') as json_data: data = json.load(json_data) json_data.close() return data def executeSensor(): sensorList = list() sensorListRaw = readSensorList() for sensor in sensorListRaw['sensors']: x = [subprocess.Popen(['powershell.exe','-ExecutionPolicy', 'Unrestricted', sensorListRaw["path"] + sensor["filename"], "-name", sensor["name"]], stdout=subprocess.PIPE, stderr=subprocess.PIPE), sensor["name"]] sensorList.append(x) return sensorList |
Json containing the path:
1 2 3 4 5 6 7 8 9 |
{ "path": "C:\\Users\\\\sensor\\", "sensors": [{ "name": "partition", "filename": "partition.ps1" }] } |
In my test environnement there were no space in the path, but now, in the production, I have some space in the path, and I’m clueless about how to execute powershell script from python with space and parameters
I tried to quote the path, without any success.
Edit: I’m sure the problem come from the powershell.exe command and not from python, because:
1 2 |
powershell.exe -ExecutionPolicy 'Unrestricted C:\Users\some space\Desktop\PythonSensor\sensor\sensor.ps1' |
Don’t work in the CMD.
Answer:
I finally found, you have to put ‘ between space like this:
1 2 |
powershell -command "C:\Users\me\Desktop\test\ps' 'test.ps1" |
In python:
1 2 |
subprocess.Popen(['powershell','-ExecutionPolicy', 'Unrestricted', '-command', path] |
with path as:
1 2 |
C:\Users\me\Desktop\some' 'space\sensor\ |
without ” as Serge Ballesta explain.