Programmatically get last boot/shutdown time

泄露秘密 提交于 2019-12-13 16:23:31

问题


I'd like to get the exact time when windows was shut down and booted. In c++ I'd simply use GetTickCount64 which retrieves the number of milliseconds that have elapsed since the system was started (thus obtaining the time by difference), but I don't know if there is an equivalent function for python and, if possible, I'd like to avoid to write a c++ module.

For last shutdown time I have no idea...maybe there is some log somewhere in windows? I tried to read the event log using win32evtlog library, but it gives me just an event and is about the dns..

edit: Ok, maybe I got a step further: I used win32evtlog, in particular calling ReadEvent log more times it gives me all logs till it returns null. Now, I need a way to understand what ids are about boot/shutdown..


回答1:


You should use the pywin32 library, and there you'll find the GetTickCount() function.

http://docs.activestate.com/activepython/2.5/pywin32/win32api__GetTickCount_meth.html

Hope this helps.




回答2:


Since this is a Windows specific question, we can leverage from both WMI and, even PowerShell. To solve your problem in PowerShell, it's just:

powershell -command "(gcim Win32_OperatingSystem).LastBootUpTime"
# Outputs: Tuesday, 9 October 2018 4:05:58 PM

To do this in Python, we can make use of subprocess:

from subprocess import call
call( ["powershell", "-command", "(gcim Win32_OperatingSystem).LastBootUpTime"] )
# Outputs: Tuesday, 9 October 2018 4:05:58 PM


来源:https://stackoverflow.com/questions/16063346/programmatically-get-last-boot-shutdown-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!