首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过Popen将值从Python脚本传递到Powershell

通过Popen将值从Python脚本传递到Powershell
EN

Stack Overflow用户
提问于 2022-08-02 09:34:13
回答 1查看 38关注 0票数 1

有点背景,我刚刚得到了一个很好的答案,我的问题,here,使我的项目进展。我正在做的是一个调用Powershell脚本的Python脚本。我正在提取python脚本输出的行,同时按照前面的答复中的建议调用它:

代码语言:javascript
运行
复制
#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脚本现在在提示符下请求用户的值,如下所示:

代码语言:javascript
运行
复制
#POWERSHELL
$VERSION = Read-Host "Enter the version number (e.g. 1.6.7.3)"

有人能帮助我理解是否可以通过Popen调用从python脚本传递$VERSION吗?我想我需要对python和Powershell脚本进行修改才能实现这一点?

提前谢谢。

编辑以澄清:我试图在脚本之后和-match正则表达式之前传递一个paramenter,但是如果删除文件名周围的括号,则不考虑匹配,即输出将是整个输出。

EN

回答 1

Stack Overflow用户

发布于 2022-08-15 20:37:57

您需要通过stdin向Read-Host PowerShellScript中的.ps1提示符提供响应,这意味着通过p.communicate()调用提供响应:

代码语言:javascript
运行
复制
version="1.6.7.3"

# ...

p_out, p_err = p.communicate(input = f"{version}\n")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73205213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档