【发布时间】:2019-11-28 14:56:13
【问题描述】:
我正在尝试运行具有 Python 3.7.3 参数的 PowerShell 脚本,但不知道如何正确调用 Popen 中的函数
我试图用我的 PowerShell 脚本做的是登录到 Cisco 路由器并根据定义的数量在 x 个路由器上运行 Cisco IOS 命令。因此,我设置 PowerShell 脚本的方式是在使用 PowerShell 时传入路由器的 IP 地址,例如.\test.ps1 177.241.87.103,或者在使用命令提示符时传入powershell.\test.ps1 177.241.87.103。这两个命令都可以工作并获得正确的输出并将其输出保存到文本文件中。
但现在我想让 Python 使用参数运行这个“test.ps1”脚本。 我已将“test.ps1”保存到“C:\Users\jgreen02”和“C:\Users\jgreen02\Desktop”
import subprocess
subprocess.call("powershell .\\test.ps1 177.241.87.103")
我确定我没有正确使用调用函数,或者我尝试运行的文件可能需要放在我的 Python 脚本所在的文件夹中。
错误输出为:
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```
【问题讨论】:
-
确保您在与文件相同的目录中运行程序。
-
这会返回什么?
import osos.getcwd() -
subprocess.call(["powershell", "-File", ".\\test.ps1", "177.241.87.103"])?
标签: python powershell command-line subprocess popen