[英]How do I run a PowerShell script with parameters from Python
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我正在尝试运行具有 Python 3.7.3 参数的 PowerShell 脚本,但不知道如何在 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.我试图用我的 PowerShell 脚本做的是登录到 Cisco 路由器并根据定义的路由器数量在 x 个路由器上运行 Cisco IOS 命令。 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.因此,我设置 PowerShell 脚本的方式是,当我使用 PowerShell 时,我传入路由器的 IP 地址,例如
.\\test.ps1 177.241.87.103
或powershell.\\test.ps1 177.241.87.103
当我使用命令时迅速的。 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.但现在我想让 Python 使用参数运行这个“test.ps1”脚本。 I've saved "test.ps1" to "C:\\Users\\jgreen02" and to "C:\\Users\\jgreen02\\Desktop"
我已将“test.ps1”保存到“C:\\Users\\jgreen02”和“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.我确定我错误地使用了调用函数,或者我尝试运行的文件可能需要放在我的 Python 脚本所在的文件夹中。
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```
2 things wrong with the approach:该方法有两个错误:
[Python.Docs]: subprocess. [Python.Docs]:子进程。 call ( args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None ) :
调用( args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None ) :
The arguments shown above are merely some common ones.
上面显示的参数只是一些常见的参数。 The full function signature is the same as that of the Popen constructor ...
完整的函数签名与Popen构造函数的签名相同...
, and specifying the arguments as a sequence rather than a string is the recommended option , 并将参数指定为序列而不是字符串是推荐的选项
When running a script, it needs to be passed via the -File option.运行脚本时,需要通过-File选项传递。 For more details, check [MS.Docs]: About PowerShell.exe (
powershell /?
)有关更多详细信息,请查看[MS.Docs]:关于 PowerShell.exe (
powershell /?
)
code00.py :代码00.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 :脚本00.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)] 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.