Page 1 of 1

[SOLVED] run(powershell script.ps1) timeout

Published: July 27, 2017 - 10:05 AM
by swbsf
Good morning,
I'm trying my luck here.
I'm creating a post-installation package that will configure the workstations by running a PowerShell script. So, to answer a potential question of "why am I not doing this post-installation in pure Python?" right away: because I already have the PowerShell script, and it's very long. Besides, there's no reason why we can't run PowerShell!
So whatever the script, it times out; it seems that psutils.Popen is not detecting the script's exit code.
For this example, I tested it with a very simple script:

Code: Select all

echo "coucou" > c:\tmp\fichier.txt
exit
Then in Python:

Code: Select all

run('powershell -NoProfile -NonInteractive -File c:/tmp/test.ps1')
I observe that the code is executed correctly, but the instruction never finishes, or rather at timeout=600.
Any ideas? What other solutions are there? I also tried it with the same error:

Code: Select all

with open('test.ps1','r') as f:
   data=f.read()
run_powershell(data)
And it's even more bizarre.
Thank you in advance!

Re: run(powershell script.ps1) timeout

Published: July 27, 2017 - 11:44 AM
by swbsf
For your information, the problem is circumvented with a:

Code: Select all

import subprocess
cmd="powershell -NoProfile -NonInteractive -File c:\\tmp\\test.ps1"
proc = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
proc.wait()
stdo,stde = proc.communicate()
print(stdo,stde)

Re: run(powershell script.ps1) timeout

Published: July 27, 2017 - 11:49 AM
by sfonteneau
Do you have PowerShell version 3 installed on this machine?

https://lists.tranquil.it/pipermail/wap ... 02340.html

Re: run(powershell script.ps1) timeout

Published: July 31, 2017 - 09:01
by swbsf
It's Windows 10, so yes.

Code: Select all

    PS C:\Windows\system32> $PSVersionTable.PSversion
     
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    5      0      10586  122

Re: run(powershell script.ps1) timeout

Published: August 1, 2017 - 09:27
by swbsf
Okay, I'm taking back what I said. The error was indeed related to the PowerShell version being less than 3.0. On one hand, it was PS 2.0, and on the other, just a slow machine, so the timeout of 3 was too short to run a simple PS command.
Thank you for your detailed help :)