Use Popen to run a Powershell script in Python, how can I get the output of a Powershell script and update it on a web page?

I am creating simple HTML with a button. When the user clicks the button, he calls the server-side Python file. In the Python file, I use Popen to call the Powershell script, as shown below:

command_line = r'"C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe" -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -c ". \"C:\DoSth\DoSth.ps1\""'
args = shlex.split(command_line)
sys.stdout.flush()
retcode = subprocess.Popen(args)

But Powershell takes a long time to complete (and generate a lot of results at runtime). After the user clicks the button, the browser displays “Wait xxx” for about a few minutes until the python code is fully executed.

Question: how can I get Powershell output at runtime and update the output in a timely manner in the browser?

+3
source share
1 answer

stdout subprocess.Popen?

- :

retcode = subprocess.Popen(args, stdout=sys.stdout)
0

Source: https://habr.com/ru/post/1784945/


All Articles