goldb.org home

AS OF MAY 2008, THIS BLOG IS NO LONGER BEING UPDATED.
Visit the new blog at: http://coreygoldberg.blogspot.com



 Tuesday, March 04, 2008

Python - Get Last Windows Reboot Date/Time

This script will output the last reboot date/time for a local Windows machine:


import re
from subprocess import Popen, PIPE

p = Popen('net statistics workstation', stdout=PIPE)
m = re.search('(\d+/\d+/\d{4}.*[A|P]M)', p.stdout.read())
if m: print 'Last Reboot: %s' % (m.group(1)) 

Output:

>> Last Reboot: 3/1/2008 1:51:41 PM





* updated the original script thanks to Ian's comment below

#    Comments [2] |
Tuesday, March 04, 2008 7:43:39 PM (Eastern Standard Time, UTC-05:00)
Had not thought to look there for boot timestamp. Thanks Corey!

The re needs a little work (it works today but won't in 6 days). It's also cheaper to process resulting p.stdout file object in one fell swoop rather than line by line.

import re
from subprocess import Popen, PIPE

p = Popen('net statistics workstation', stdout=PIPE)
m = re.search('(\d+/\d+/\d{4}.*[A|P]M)', p.stdout.read())
if m: print 'last boot: %s' % (m.group(1))

Cheers!
Tuesday, March 04, 2008 8:43:50 PM (Eastern Standard Time, UTC-05:00)
@Ian:

thanks for the tips! updated the example in my post.

-Corey
Comments are closed.