[英]Get reboot history with python in Windows
I was trying to get the reboot historical from a Windows 10 computer with python but I'm affraid I can't read event-viewer.我试图从带有 python 的 Windows 10 计算机获取重启历史记录,但我担心我无法读取事件查看器。
Is there any option to get something similar to this powershell line?是否有任何选项可以获得类似于此 powershell 线的东西?
get-eventlog system | where-object {$_.eventid -eq 1074} | select Timegenerated, EntryType, Message
the main idea is to do this "query" to a computer's list in local network.主要思想是对本地网络中的计算机列表进行此“查询”。
The simplest approach is to call PowerShell's CLI from Python : Windows PowerShell: powershell.exe
;最简单的方法是从 Python 调用 PowerShell 的 CLI :Windows PowerShell:
powershell.exe
。 PowerShell [Core] v6+: pwsh.exe
. PowerShell [核心] v6+:
pwsh.exe
。
The following Python 3.6+ solution uses powershell.exe
以下Python 3.6+解决方案使用
powershell.exe
# Python 3.6+
# (Solutions for earlier versions are possible.)
import subprocess
output = subprocess.run([
'powershell.exe',
'-noprofile',
'-executionpolicy',
'-bypass',
'-c',
'get-eventlog system | where-object {$_.eventid -eq 1074} | select Timegenerated, EntryType, Message'
],
capture_output=True)
# CAVEAT: The *system*'s OEM code page is assumed for decoding the
# raw captured stdout output to text.
# Any in-session changes to the active OEM code page via `chcp`
# are NOT recognized; e.g., if you've changed to page 65001 (UTF-8)
# you must use 'utf-8' explicitly.
print(output.stdout.decode('oem'))
Pros and cons:优点和缺点:
The advantage of this approach is that you can reuse the existing PowerShell command as-is, which gives all the high-level functionality that PowerShell has to offer.这种方法的优点是您可以按原样重用现有的 PowerShell 命令,它提供了 PowerShell 必须提供的所有高级功能。
The disadvantages are:缺点是:
Slow performance, due to the overhead of launching a PowerShell process (though in the case of a long-running process such as this one that probably won't matter)由于启动 PowerShell 进程的开销,性能缓慢(尽管对于像这个这样的长时间运行的进程,这可能无关紧要)
The need to parse the for-display command output returned from the PowerShell command.需要解析PowerShell命令返回的for-display命令output。 Conceivably, you can pass
-of XML
in order to make PowerShell output CLIXML and parse the XML in Python);可以想象,您可以通过
-of XML
来生成 PowerShell output CLIXML 并解析 Python 中的 Z35008671);5B9CFED1)5B9CFED1) a simpler option is to modify the Powershell command to return more structured output, such as appending | ConvertTo-Csv
更简单的选择是修改 Powershell 命令以返回更结构化的 output,例如追加
| ConvertTo-Csv
| ConvertTo-Csv
or | ConvertTo-Csv
或| ConvertTo-Json
| ConvertTo-Json
to the command. | ConvertTo-Json
到命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.