I am trying to start a Powershell subprocess from Python. I need to send Powershell code from Python to a child process. I'm so far away:
import subprocess
import time
args = ["powershell", "-NoProfile", "-InputFormat None", "-NonInteractive"]
startTime = time.time()
process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.stdin.write("Write-Host 'FINISHED';".encode("utf-8"))
result = ''
while 'FINISHED' not in result:
result += process.stdout.read(32).decode('utf-8')
if time.time() > startTime + 5:
raise TimeoutError(result)
print(result)
This time because nothing is written to stdout. I think the cmdlet Write-Hostwill never be executed. Even simple bash / Cygwin code echo "Write-Host 'FINISHED';" | powershelldoes not seem to do the job.
For comparison, sending a block of code using the flag -Commandis correct.
How can I convince Powershell to run the code I submit to stdin?
source
share