在Windows中使用python获取重新启动历史记录

在Windows中使用python获取重新启动历史记录,python,powershell,reboot,Python,Powershell,Reboot,我试图用python从Windows10计算机上获取重新启动的历史记录,但我很惊讶我无法读取事件查看器 是否有任何选项可以获得类似于此powershell系列的产品 get-eventlog system | where-object {$_.eventid -eq 1074} | select Timegenerated, EntryType, Message 其主要思想是对本地网络中的计算机列表执行此“查询”。最简单的方法是从Python调用PowerShell的CLI:Windows P

我试图用python从Windows10计算机上获取重新启动的历史记录,但我很惊讶我无法读取事件查看器

是否有任何选项可以获得类似于此powershell系列的产品

get-eventlog system | where-object {$_.eventid -eq 1074} | select Timegenerated, EntryType, Message

其主要思想是对本地网络中的计算机列表执行此“查询”。

最简单的方法是从Python调用PowerShell的CLI:Windows PowerShell:;PowerShell[Core]v6+:

以下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'))
优点和缺点:

  • 这种方法的优点是可以按原样重用现有的PowerShell命令,这提供了PowerShell必须提供的所有高级功能

  • 缺点是:

    • 性能低下,这是由于启动PowerShell进程的开销造成的(尽管对于像这样的长时间运行的进程来说,这可能无关紧要)

    • 需要解析从PowerShell命令返回的display命令输出。可以想象,您可以传递XML的
      ,以使PowerShell输出CLIXML并用Python解析XML);一个更简单的选项是修改Powershell命令以返回更结构化的输出,例如将
      |ConvertTo Csv

      |将转换为Json
      转换为命令


    • 最简单的方法是从Python调用PowerShell的CLI:Windows PowerShell:;PowerShell[Core]v6+:

      以下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'))
      
      优点和缺点:

      • 这种方法的优点是可以按原样重用现有的PowerShell命令,这提供了PowerShell必须提供的所有高级功能

      • 缺点是:

        • 性能低下,这是由于启动PowerShell进程的开销造成的(尽管对于像这样的长时间运行的进程来说,这可能无关紧要)

        • 需要解析从PowerShell命令返回的display命令输出。可以想象,您可以传递XML的
          ,以使PowerShell输出CLIXML并用Python解析XML);一个更简单的选项是修改Powershell命令以返回更结构化的输出,例如将
          |ConvertTo Csv

          |将转换为Json
          转换为命令


      您可以从Python:Windows PowerShell:调用PowerShell的CLI;PowerShell[Core]v6+:。您可能想看看这个>>>python-读取特定的Windows事件日志事件-堆栈溢出-@mklement0您必须使用pythons模块在python中运行PowerShell。使用python库(如
      pywin32
      )与COM对象进行本机交互可能更安全。@RoadRunner:是的,但这与您调用的PowerShell命令本身一样安全或不安全。这种方法的优点是可以按原样重用现有的PowerShell命令。缺点是性能(尽管对于像这样的长时间运行的进程来说,这可能无关紧要)和需要解析PowerShell命令的文本输出(尽管可以想象,您可以传递
      -of XML
      以输出CLIXML并解析Python中的XML)。您可以从Python:Windows PowerShell:调用PowerShell的CLI;PowerShell[Core]v6+:。您可能想看看这个>>>python-读取特定的Windows事件日志事件-堆栈溢出-@mklement0您必须使用pythons模块在python中运行PowerShell。使用python库(如
      pywin32
      )与COM对象进行本机交互可能更安全。@RoadRunner:是的,但这与您调用的PowerShell命令本身一样安全或不安全。这种方法的优点是可以按原样重用现有的PowerShell命令。缺点是性能(尽管对于像这样的长时间运行的进程来说,这可能无关紧要)和需要解析PowerShell命令的文本输出(尽管可以想象,您可以将XML的
      -of
      传递到输出CLIXML并在Python中解析XML)。