【问题标题】:How do I run a PowerShell script with parameters from Python如何使用 Python 中的参数运行 PowerShell 脚本
【发布时间】: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


【解决方案1】:

该方法有 2 个问题:

code00.py

#!/usr/bin/env python3

import sys
import subprocess


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


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

script00.ps1

$PSVersionTable

输出

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)] 64bit 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.

【讨论】:

  • @Jgreene44:这回答了你的问题吗?如果是,请接受答案,以便其他人也可以接受。 ([SO]: What should I do when someone answers my question?)。
  • 它在 Python 中输出 VersionTable,但它并没有结束 subprocess.call() 函数。它从不打印“Powershell 返回...”或“完成”,我没有更改您发布的任何内容。它只是挂在 subprocess.call(cmd) 行
  • 那么,test.exe 可执行文件可能没有(正确地)结束。看来它仍在等待......无论如何,如果这不能回答你的问题,你不应该把自己扔到一些显然可以解决它的答案。
猜你喜欢
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 2019-04-17
  • 1970-01-01
  • 2014-03-23
  • 2013-05-19
相关资源
最近更新 更多