【发布时间】:2021-02-24 19:38:25
【问题描述】:
我有一个非常简单的脚本,它使用 Python 的 subprocess 库打印出 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 捕获输出。
【问题讨论】:
-
您是否将
capture_output标志设置为True?默认为False,与设置stdout不兼容。 -
我尝试按照您的建议将
capture_output设置为True(由于ValueError必须删除stdout),但仍然给我bad 输出(b'Get-MpComputerStatus\r\n')。 -
是的,我说这些标志不兼容。
标签: python powershell subprocess output