2

I have a Python script and I want to run a PowerShell cmdlet. I've looked online and the only thing I can find is running a PowerShell script, but I feel like writing a cmdlet to a script and then dot sourcing it for execution would take a lot longer than needed.

I've tried using subprocess.Popen in the following way:

cmd = subprocess.Popen(['C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe', ps_cmdlet])

But ps_cmdlet is a python string variable with a powershell cmdlet as its value. So, I'm obviously getting a "No such file or directory" error. Is there any way to run a powershell cmdlet in a python script without using things like IronPython?

Thanks!

3 Answers 3

3

This works rather well

import subprocess
pl = subprocess.Popen(['powershell', 'get-process'], stdout=subprocess.PIPE).communicate()[0]
print(pl.decode('utf-8'))
Sign up to request clarification or add additional context in comments.

1 Comment

WIth PowerShell Core 7, I'm doing ['pwsh', '-Command', 'Get-Process'] and it works perfectly! I don't need to detect the version or location of the .exe.
0

Try the following (ps_cmdlet is a python string):

subprocess.call(ps_cmdlet)

edit: Here is an example that will output your machine's ip configuration to Powershell:

ps_cmdlet = 'ipconfig'
subprocess.call(ps_cmdlet)

another edit: Another way that works for me is:

ps_cmdlet = 'whatever command you would enter in powershell'
p = subprocess.Popen(ps_cmdlet,stdout=subprocess.PIPE)
p.communicate()

6 Comments

I'm still getting a "No such file or directory error". I'm just trying: import subprocess cmdlet = "start-service lanmanserver" subprocess.call(cmdlet)
Are you sure the error is occuring at subprocess.call?
Traceback (most recent call last): File "test.py", line 4, in ? subprocess.call(ps_cmdlet) File "/usr/lib64/python2.4/subprocess.py", line 419, in call return Popen(*args, **kwargs).wait() File "/usr/lib64/python2.4/subprocess.py", line 550, in init errread, errwrite) File "/usr/lib64/python2.4/subprocess.py", line 996, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory
This isn't a cmdlet example - ipconfig is an executable sitting on disk. It has nothing to do with powershell.
Try doubling up on the backslashes in your initial example - "C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe". \ needs to be escaped in Python strings...
|
-1
import subprocess
process = subprocess.Popen([r"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe", "get-process"],
                           shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
process_output = process.read().splitlines()

Above script would help in executing PS Cmdlets from Python.

Comments

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.