2

I keep trying to run this piece of code but everytime I print, it seems to net me nothing in regards to what I see with my output.

p = subprocess.run(["powershell.exe", "C://Users//xxxxx//Documents//betterNetstatOut.ps1"], shell=True, capture_output=True, text=True)

output = p.stdout
print(output)

My PowerShell command is a very basic println at this point:

Write-Output 'Hello world'

but running print on my out seems to return an empty string. I also tried running subprocess.Popen() and subprocess.call() and they all seem to return an empty string instead of 'Hello World'. Eventually, I would like to parse many lines and move them to a dataframe but I am stuck on this one line first.

1
  • try with ["powershell.exe", "-File", "path//to//file"]... Commented Sep 2, 2022 at 19:16

2 Answers 2

3

Your PowerShell command likely produced only stderr output, which is why you saw no output given that you only printed p.stdout - also printing p.stderr would surface any stderr output (which typically contains error messages).

Assuming your script file path is correct, the likeliest explanation for receiving only stderr output is that your effective PowerShell execution policy prevents execution of scripts (.ps1 files), which you can bypass with -ExecutionPolicy Bypass in a call to the Windows PowerShell CLI, powershell.exe.

Additionally:

  • There's no need for double slashes (//) in your script path; while it still works, / is sufficient.

  • It's better to use the -File parameter rather than -Command (which is implied) for invoking scripts via the PowerShell CLI - see this answer for more information.

  • For a predictably execution environment and to avoid unnecessary overhead from loading profiles, using -NoProfile is advisable.

  • You don't need shell=True, which, due to calling via cmd.exe, only slows your command down.

To put it all together:

import subprocess

p = subprocess.run(
     ["powershell.exe", 
      "-NoProfile", 
      "-ExecutionPolicy", "Bypass", 
      "-File", "C:/Users/xxxxx/Documents/betterNetstatOut.ps1"], 
     capture_output=True, text=True
    )

print('--- stdout --')
print(p.stdout)
print('--- stderr --')
print(p.stderr)
Sign up to request clarification or add additional context in comments.

7 Comments

Follow-up question: Is there a way to handle the PowerShell command without looking for a .ps1 file? Just run 'netstat -a' directly from the subprocess.run() function?
@ZarifRahman, use -Command instead of -File, and pass 'netstat -a' to it. If you need a comprehensive overview of the PowerShell CLI, see this answer. That said, if all you need is a call to netstat, you don't need PowerShell at all - just use 'netstat.exe' instead of "powershell.exe" and '-a' as its argument.
Thank you for the example! But 'netstat -a' is just an example I wanted to run through.
@ZarifRahman, understood. Then use the -Command approach.
Yup! Haha, from my research, this issue seemed a little bit more complicated than in this original question. Didn't think this would be a bigger deal when posting this comment but slowly realized what is going on behind the scenes.
|
-1
import subprocess

p = subprocess.run(["powershell.exe", "powershell -ExecutionPolicy Bypass -File", "C://Users//xxxxx//Documents//betterNetstatOut.ps1"], shell=True, capture_output=True, text=True)
print(p.stdout)

1 Comment

-ExecutionPolicy Bypass is a good pointer, but Is the nesting of powershell.exe calls in your code a typo?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.