Phate
Phate

Reputation: 6612

Programmatically get last boot/shutdown time

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..

Upvotes: 2

Views: 3654

Answers (2)

Stephen Quan
Stephen Quan

Reputation: 25956

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

Upvotes: 0

Baltasarq
Baltasarq

Reputation: 12212

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.

Upvotes: 1

Related Questions