I want to initalise a powershell subprocess and be able to send commands to it and then receive its output.
Slightly different from the usual subprocess.Popen(['powershell', '...']) use case because ideally I would like my powershell session to persist whilst my script is running.
This is because I might need to run 100+ commands (Get-ADUser, etc..) and the startup costs of loading the powershell console and loading the Active directory module is quite significant.
Roughly speaking I was imagining something like the following (though I'm aware this does not work):
class ActiveDirectory:
def __init__(self):
self.proc = subprocess.Popen(['powershell'], stdin.., stdout..)
def run_command(self, command):
output = self.proc.communicate(command)
return output
I can't find an obvious way to do this using the subprocess module.
Does anybody have any idea how subprocess can be manipulated to achieve this? Or is there any other tool that provide a richer interface into sub processes? Possibly twisted.reactor?
Thanks, O.