The script for today is created for Paul.
It is an extension of the previous server uptime script.
The uptime is now formatted in: xx days, xx hours, xx minutes.
Follow the next steps to run the script (admin rights needed for the WMI connections):
- copy and paste the script in your favorite text editor
- save the script (for example c:tempserveruptimev2.vbs)
- open a command prompt
- go to “c:temp”
- give “cscript serveruptimev2.vbs” (without quotes) and enter
The script:
' Name : serveruptimev2.vbs
' Description : script to enumerate the system uptime of all servers in Active Directory V2
' Author : dirk adamsky - deludi bv
' Version : 2.00
' Date : 15-07-2010
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=computer)(operatingSystem=*server*))"
strAttributes = "name"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
Set adoRecordset = adoCommand.Execute
Do Until adoRecordset.EOF
On Error Resume Next
strHostname = adoRecordset.Fields("name").Value
If CheckStatus(strHostname) = False Then
Wscript.Echo strHostname & " does not reply"
Else
Wscript.Echo strHostname & " is up for " & GetUptime(strHostname)
End If
adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close
Set adoRecordset = Nothing
Set objRootDSE = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing
Function CheckStatus(strAddress)
Dim objPing, objRetStatus
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_PingStatus where address = '" & strAddress & "'")
For Each objRetStatus In objPing
If IsNull(objRetStatus.StatusCode) Or objRetStatus.StatusCode <> 0 Then
CheckStatus = False
Else
CheckStatus = True
End If
Next
Set objPing = Nothing
End Function
Function GetUptime(strServer)
Set objDateTime = CreateObject("WbemScripting.SWbemDateTime")
Set objWMIService = GetObject("winmgmts:\" & strServer & "rootcimv2")
Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
objDateTime.Value = objOS.LastBootUpTime
strMinutes = DateDiff("n", objDateTime.GetVarDate, Now)
If strMinutes =< 0 Then
strUptime = "0 days, 0 hours, 0 minutes"
Else
strUptime = ""
If strMinutes >= 1440 Then
strUptime = Round(strMinutes1440,0) & " days,"
End If
strMinutes = strMinutes Mod 1440
If strMinutes >= 60 Then
strUptime = strUptime & (strMinutes60) & " hours,"
End If
strMinutes = strMinutes Mod 60
GetUptime = strUptime & strMinutes & " minutes"
End If
Next
Set colOperatingSystems = Nothing
Set objWMIService = Nothing
Set objDateTime = Nothing
End Function
When you have problems/questions please post a reply or give a ‘star’ rating.
Happy scripting.
Best regards,
Dirk Adamsky – Deludi BV
[adrotate group="2" banner="3"]

Hello Mate,
Script is awesome!!!
I request you to do some more modification, so that it will help people like me..(Monthly Reporting)
Date function (30days and 31days)
Calling server names from Txt File..
And a HTML or Excel out put with the details (Percentage of uptime, uptime in Hours and Days).
Thanks a lot in advance..
VJ.
Hi Vijay,
with this script you can find the uptime of a single server:
strComputer = "srv001" Set dateTime = CreateObject("WbemScripting.SWbemDateTime") Set objWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2") Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem") For Each objOS in colOperatingSystems dateTime.Value = objOS.LastBootUpTime wscript.echo dateTime.GetVarDate NextPlease replace “srv001″ with your servername.
The other questions will be answered later..
Best regards,
Dirk Adamsky
Hello Dirk,
The scripts returns only “SERVERNAME does not reply”.
Do you know why or what it is missing from my part?
Thanks!