python - 子进程不会捕获 powershell 输出
问题描述
subprocess
我有一个非常简单的脚本,它使用 Python 的库打印出 powershell 输出:
import subprocess
out = subprocess.run(
['powershell', '-command', '"Get-MpComputerStatus"'],
stdout = subprocess.PIPE,
shell = True
)
print(out.stdout)
我打印出来的输出b'Get-MpComputerStatus\r\n'
是错误的。
预期的输出是各种计算机统计信息的列表(通过运行自行查看powershell -command "Get-MpComputerStatus"
)。
我也尝试os.system('powershell -command "Get-MpComputerStatus"')
了哪个有效,但我无法使用os.system
.
解决方案
不应该用Get-MpComputerStatus
引号括起来。
import subprocess
out = subprocess.run(
['powershell', '-command', 'Get-MpComputerStatus'],
stdout = subprocess.PIPE
)
print(out.stdout)
UPD:事实证明,这标志着capture_output
并且text
没有必要。引号中的 PowerShell 命令是唯一的问题。我也同意@tripleee 的观点shell
。在这里阅读更多。
推荐阅读
- google-cloud-platform - JobStatistics 中“waitMsAvg”的确切含义是什么?
- dnsmasq - 动态重新加载转发规则而不弹跳 dnsmasq
- api - 在 GitHub api 中超出 API 限制
- google-apps-script - 执行后 Google Apps 脚本从数组中删除数据
- selenium - Web Scraping:从网页上的图表中悬停信息
- python - matplotlip图如何匹配cv2的坐标 蟒蛇 |
- css - scss 变量不返回 css 变量值
- amazon-web-services - 我无法使用 curl https://install.meteor.com/ 在 ubuntu 上安装流星 | 嘘
- kotlin - 未为 recyclerview 中的 viewpager 片段创建视图
- excel - 搜索随机数据并填写另一张纸