Thursday, January 22, 2009

Last Boot Times.  A Microsoft.public.windows.powershell forum question made me think about how important historical knowledge of last boot times are to most administrators. Microsoft has an excellent utility (uptime.exe at http://support.microsoft.com/kb/232243) that records significant system events and estimates system availability. I wanted to see how difficult such a utility would be to recreate in Powershell. I was surprised to find some reporting differeneces for last uptime between the different approaches. To be continued...

##. \LastBootWMI.ps1
$colItems = Gwmi Win32_OperatingSystem -Namespace "root\CIMV2" -ComputerName localhost
$LB = $ColItems.ConvertToDateTime($ColItems.LastBootUpTime)
$array = (get-date).subtract($LB)
$Curr_date = (get-date) 
$days = $array.days
$hours = $array.hours
$minutes = $array.minutes
$seconds = $array.seconds
write "Last Boot Time: $LB Current Time: $Curr_date" 
write "Time from Last Boot in Days.Hours.Minutes.Seconds = $days.$hours.$minutes.$seconds"

PS >.\LastBootWMI.ps1
Last Boot Time: 01/20/2009 03:42:31 Current Time: 01/22/2009 19:00:25
Time from Last Boot in Days.Hours.Minutes.Seconds = 2.15.17.53


## .\LastBootTimes.ps1
$SysEvtLog = get-eventlog -log System
$Evt_ID_6009 = $SysEvtLog | ?{$_.eventID -eq 6009}
$Evt_ID_6009TG = $Evt_ID_6009 | %{$_.TimeGenerated}
$count = ($Evt_ID_6009TG.count) -1
$curr_date = get-date
$array = $Evt_ID_6009TG[0..$count] | %{($curr_date - $_)}
write "Last boot Date/Time -- LBTs from Current Time in Days.Hours.Minutes.Seconds"
0..$count | %{
$days = $array[$_].days;
$hours = $array[$_].hours;
$minutes = $array[$_].minutes;
$seconds = $array[$_].seconds;
$date = $Evt_ID_6009TG[$_];
write "$date -- $days.$hours.$minutes.$seconds";}

PS >.\LastBootTimes.ps1
Last boot Date/Time -- LBTs from Current Time in Days.Hours.Minutes.Seconds
01/15/2009 22:40:17 -- 6.20.20.5
01/09/2009 09:52:51 -- 13.9.7.31
01/07/2009 17:39:39 -- 15.1.20.43
01/05/2009 18:30:06 -- 17.0.30.16
01/05/2009 11:01:59 -- 17.7.58.23
12/24/2008 11:20:21 -- 29.7.40.1
12/21/2008 10:01:15 -- 32.8.59.7
12/19/2008 09:23:52 -- 34.9.36.30
12/11/2008 08:04:59 -- 42.10.55.23
12/03/2008 08:10:23 -- 50.10.49.59


D:\>uptime.exe /s
Uptime Report for: \\RMFMEDIA

Current OS: Microsoft Windows XP, Service Pack 3, Multiprocessor Free.
Time Zone: Pacific Standard Time

System Events as of 1/22/2009 7:13:12 PM:

Date:      Time:        Event:               Comment:
---------- -----------  -------------------  -----------------------------------
 12/3/2008  8:10:23 AM  Boot
12/11/2008  8:03:46 AM  Shutdown             Prior uptime:7d 23h:53m:23s
12/11/2008  8:04:59 AM  Boot                 Prior downtime:0d 0h:1m:13s
12/19/2008  9:22:42 AM  Shutdown             Prior uptime:8d 1h:17m:43s
12/19/2008  9:23:52 AM  Boot                 Prior downtime:0d 0h:1m:10s
12/21/2008 10:01:15 AM  Boot
12/24/2008 11:20:21 AM  Boot
  1/5/2009 11:00:36 AM  Shutdown             Prior uptime:11d 23h:40m:15s
  1/5/2009 11:01:59 AM  Boot                 Prior downtime:0d 0h:1m:23s
  1/5/2009  6:30:06 PM  Boot
  1/5/2009  6:30:12 PM  Bluescreen           STOP 0x0000008e
  1/7/2009  5:38:05 PM  Shutdown
  1/7/2009  5:39:39 PM  Boot                 Prior downtime:0d 0h:1m:34s
  1/9/2009  9:52:04 AM  Shutdown             Prior uptime:1d 16h:12m:25s
  1/9/2009  9:52:51 AM  Boot                 Prior downtime:0d 0h:0m:47s
 1/15/2009  9:28:14 AM  Shutdown             Prior uptime:5d 23h:35m:23s
 1/15/2009 10:40:17 PM  Boot                 Prior downtime:0d 13h:12m:3s

Current System Uptime: 6 day(s), 20 hour(s), 33 minute(s), 32 second(s)

No comments: