I want to initialise a powershell subprocess and be able to send commands to it and receive its output using Python on Windows.
Please can someone help with simple examples? I have gone through almost every other post similar to this on here but haven't had any luck with my specific problem...
I am to run this code to automate a current manual process conducted on Powershell.
This is a high level look at what I want to achieve using Python
- I want to launch a powershell session using Python
- Then CD into a directory
- Run a powershell script in that directory
- Display/retrieve all outputs
- And pass new commands in the same session, again, whilst capturing outputs
So far all I have is the following code which launches powershell and runs a .ps1 file by giving directory. I'm not sure how to proceed from here or if this is even the best approach given the steps I need to undertake shown above.
p = subprocess.Popen(["powershell.exe", Powershell.exe -executionpolicy remotesigned -File 'G:\My Documents\helloworld.ps1';"]. stdout=sys.stdout)
p.communicate()
After running the above code, python terminal displays the message 'Test Message' correctly