2

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

Phate
  • 6,066
  • 15
  • 73
  • 138
  • 1
    You can access Win32 API calls with the [`pywin32`](http://sourceforge.net/projects/pywin32/) library. Regarding the last shutdown, it seems related to event with id 6006([source](http://www.guidingtech.com/432/last-shutdown-time-windows/)). – Bakuriu Apr 17 '13 at 15:04

2 Answers2

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.

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
0

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
Stephen Quan
  • 21,481
  • 4
  • 88
  • 75