有点背景,我刚刚得到了一个很好的答案,我的问题,here,使我的项目进展。我正在做的是一个调用Powershell脚本的Python脚本。我正在提取python脚本输出的行,同时按照前面的答复中的建议调用它:
#PYTHON
import subprocess
p = subprocess.Popen(
['powershell', '-NoProfile', '-Command', "(./script.ps1) -match '^https://'" ],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True
)
# Wait for the process to terminate and collect its stdout and stderr output.
p_out, p_err = p.communicate()
# Split the single multi-line string that contains the links
# into individual lines.
lines = p_out.splitlines()
print(lines)
我想要做的是将python脚本生成的某些值传递给powershell脚本。例如,powershell脚本现在在提示符下请求用户的值,如下所示:
#POWERSHELL
$VERSION = Read-Host "Enter the version number (e.g. 1.6.7.3)"
有人能帮助我理解是否可以通过Popen调用从python脚本传递$VERSION吗?我想我需要对python和Powershell脚本进行修改才能实现这一点?
提前谢谢。
编辑以澄清:我试图在脚本之后和-match正则表达式之前传递一个paramenter,但是如果删除文件名周围的括号,则不考虑匹配,即输出将是整个输出。
发布于 2022-08-15 20:37:57
您需要通过stdin向Read-Host
PowerShellScript中的.ps1
提示符提供响应,这意味着通过p.communicate()
调用提供响应:
version="1.6.7.3"
# ...
p_out, p_err = p.communicate(input = f"{version}\n")
https://stackoverflow.com/questions/73205213
复制相似问题