I'm trying to write a Python program that will open a powershell process, then keep it open while passing commands to it and getting the output. I don't want to open a new powershell process for each command because the powershell session must authenticate with a remote system, and I don't want to constantly go through the not-super-fast authentication process with every command.
What's happening though is that the powershell process is taking over and blocking continued execution of the Python code... as soon as powershell process starts, the Python process halts and waits for it to exit. Obviously this is no good because as soon as powershell launches I can't pass any commands to it.
The relevant portion of my code is:
import subprocess as sp
def _open_powershell_session():
ps_path = r"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe"
azure_path = r"C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\ShortcutStartup.ps1"
flags = ["-NoExit", "-ExecutionPolicy", "Bypass", "-File"]
launch_str = [ps_path] + flags + [azure_path]
ps_session = sp.Popen(args=launch_str, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE)
return ps_session
What am I doing wrong?