Home:ALL Converter>How do I run a PowerShell script with parameters from Python

How do I run a PowerShell script with parameters from Python

Ask Time:2019-07-19T23:19:39         Author:Jgreene44

Json Formatter

I'm trying to run PowerShell scripts that have parameters from Python 3.7.3, but don't know how to properly call the function in Popen

What I'm trying to do with my PowerShell script is login to Cisco routers and run Cisco IOS commands on x number of routers based on how many are defined. So the way I have my PowerShell script setup I pass in the IP address of the router like .\test.ps1 177.241.87.103 when I'm using PowerShell, or powershell.\test.ps1 177.241.87.103 when I'm using command prompt. Both of these commands work and get the correct output and save their outputs to text files as well.

But now I want to get Python to run this "test.ps1" script with the parameter. I've saved "test.ps1" to "C:\Users\jgreen02" and to "C:\Users\jgreen02\Desktop"

import subprocess

subprocess.call("powershell .\\test.ps1 177.241.87.103")

I'm certain I'm using the call function incorrectly, or maybe the file I'm trying to run needs to be placed in the folder where my Python script is sitting.

The error output is:

Traceback (most recent call last):
  File "C:/Users/jgreen02/PycharmProjects/PortChecker/Platypus.py", line 43, in <module>
    subprocess.call(["powershell test.ps1 10.238.241.38"])
  File "C:\Users\jgreen02\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 323, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Users\jgreen02\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\jgreen02\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified```

Author:Jgreene44,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57115405/how-do-i-run-a-powershell-script-with-parameters-from-python
yy