Thursday, October 23, 2008

[vbscript] Quick & Dirty "when was my server last rebooted"

If you are like me you find yourself rebooting servers on a regular basis, or in the case of say emergency patching in an enterprise and you are impatient when it comes to waiting for SMS or some other deployment system to tell you when your systems have been rebooted - here is a quick and dirty solution to your administrative woes.

1: create a text file with your servers (or PCs) at c:\scripts\servers.txt, place each Windows system in the txt file on a separate line

2: copy the following script to s-uptime.vbs
' Quick and Dirty Script to confirm last boot time of server
' Requires a text file of your servers (or any windows systems) at c:\scripts\servers.txt - alter as you see fit
' 10/23/08 -

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile ("c:\scripts\servers.txt", ForReading)

Do While Not objFile.AtEndOfStream
strComputer = objFile.ReadLine
set objWMIDateTime = CreateObject("WbemScripting.SWbemDateTime")
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colServer = objWMI.InstancesOf("Win32_OperatingSystem")
for each objServer in colServer
objWMIDateTime.Value = objServer.LastBootUpTime
Wscript.Echo strComputer & " Last Boot Time: " & objWMIDateTime.GetVarDate


3: Run your script for output to the console (if cscript is not your default script processor you will be bombarded with wscript pop-up windows, so instead run this script at a cmd prompt > cscript s-uptime.vbs) Output should look like this:

server001 Last Boot Time: 10/23/2008 6:51:32 PM
server002 Last Boot Time: 10/23/2008 6:42:08 PM
server003 Last Boot Time: 10/23/2008 11:49:15 AM

No comments:

Post a Comment