0

I have been having a strange issue. For work I am wanting to open a powershell file through python due to various reasons. I have been stuck on how to do this because I have tried everything. Well I have coworker (that works in a much different timezone) email me saying to do this and how it worked for him:

import subprocess as sub


def runMain():

    ab = sub.Popen(["powershell.exe", "C:\\TestAutomation\\ePuse\\ePuse.ps1"], stdout=sub.PIPE)

    ab.communicate()


if __name__ == '__main__':
    runMain()

He even showed me a screenshot verifying it worked for him. However, I have tried his code and it's not working for me at all. It just keeps outputting "process finished with exit code 0"

Does anyone have any idea on what is going on here ? I really can't understand what I'm doing wrong. I don't know what version of python he is using, but I'm using 2.7 (I was advised too). Could it be another version of python he is using ? Why is my code not outputting anything ? Thanks for the help.

SaturnsBelt
  • 251
  • 2
  • 9
  • Seems like everything is running as it should. Your script is not goig to output anything, when you do not specify it.. Try `print ab.communicate()`. This should get you some output – rettenda Jul 25 '19 at 14:22
  • Exit code 0 is also a successful exit code, so it ran without problem. – Neijwiert Jul 25 '19 at 14:23
  • @Neijwiert but why can't I see the output ? Do you have any idea ? – SaturnsBelt Jul 25 '19 at 14:24
  • @SaturnsBelt you tried using the print statement? – rettenda Jul 25 '19 at 14:36
  • @rettenda Yes I have. It's baffling really. It must be the IDE or my version of python. Weird... – SaturnsBelt Jul 25 '19 at 14:39
  • what is the expected output ? – Corentin Limier Jul 25 '19 at 14:46
  • I had some issues using `Popen` in the past.. Is your Powershell script generating output immediately or is it doing some processing first? `communicate()` just reads stdout. When there's no output yet, it's possible your script ends before the subprocess actually is returning something – rettenda Jul 25 '19 at 14:58
  • What happens when you open a PowerShell prompt and run the PowerShell script (`C:\TestAutomation\ePuse\ePuse.ps1`) from there? – iRon Jul 25 '19 at 16:48
  • Is your execution policy configured to allow you to run scripts? – Bill_Stewart Jul 25 '19 at 19:20

0 Answers0