3

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```
3
  • Make sure you're running the program in the same directory as the file. Commented Jul 19, 2019 at 15:21
  • What does this return? import os os.getcwd() Commented Jul 19, 2019 at 15:23
  • subprocess.call(["powershell", "-File", ".\\test.ps1", "177.241.87.103"])? Commented Jul 19, 2019 at 15:27

1 Answer 1

2

2 things wrong with the approach:

code00.py:

#!/usr/bin/env python

import subprocess
import sys


def main(*argv):
    cmd = ["PowerShell", "-ExecutionPolicy", "Unrestricted", "-File", ".\\script00.ps1"]  # Specify relative or absolute path to the script
    ec = subprocess.call(cmd)
    print("Powershell returned: {:d}".format(ec))


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.\n")
    sys.exit(rc)

script00.ps1:

${PSVersionTable}

Output:

cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q057115405]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code00.py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 064bit on win32


Name                           Value
----                           -----
PSVersion                      5.1.18362.145
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.145
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


Powershell returned: 0

Done.
Sign up to request clarification or add additional context in comments.

2 Comments

It outputs the VersionTable in Python, but then it doesn't end the subprocess.call() function. It never prints "Powershell returned..." or "Done", I haven't altered anything that you posted. It just gets hung up on the subprocess.call(cmd) line
Then probably, the test.exe executable didn't end (properly). It appears it's still waiting.... And anyway, if this doesn't answer your question, you shouldn't throw yourself away to some answer that apparently solves it.

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.