In large Active Directory environments it is useful to know the uptime of each server (for patches, etc.).
This script lists all servers in Active Directory, checks if they are alive and if so check their uptime.
It can be run as a scheduled task (for example every day or week).
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:tempserveruptime.vbs)
- open a command prompt
- go to “c:temp”
- give “cscript serveruptime.vbs” (without quotes) and enter
The script:
' Name : serveruptime.vbs
' Description : script to monitor all servers in Active Directory
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 17-03-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
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) & " days"
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
GetUptime = DateDiff("d", objDateTime.GetVarDate, Now)
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="3"]

[...] Active Directory and WMI: VBscript to enumerate the system uptime … [...]
Only problem I see is that if any server is not pingable at the time, your script blows up
c:uptime.vbs(60, 2) Microsoft VBScript runtime error: The remote server machine does not exist or is unavailable: ‘GetObject’
Hi Jamie,
Sorry for my late reply (missed your mail).
The target server did reply because the checkstatus function was already passed.
The error is on line 60 that is when the function getuptime starts.
The previous function checkstatus already made a WMI conection with the target server,
so making WMI connections is possible.
Maybe the WMI implementation on the target server has some errors (maybe that specific namespace is corrupt).
I run the script daily on several hundreds of w2k3 machines, it is rather stable.
On the other hand: error handling is not my favorite thing…
Hope the above gives some hints to solve the probem.
Happy scripting.
Best regards,
Dirk Adamsky
Hi! what should I modify on the script if I would like to see the uptime expressed days, minutes and seconds? Thank you for your great job!
- Paul
[...] The Netherlands – Brazil 2 – 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!! Recent CommentsPaul on Active Directory and WMI: VBscript to enumerate the system uptime of all servers in Active DirectoryDK on Active Directory: VBscript to enumerate the roaming profile size of all users in Active [...]
Hi Paul,
I have updated the script.
The Getuptime function now gives the uptime as xx days, xx hours, xx minutes.
You can find the script here:
http://deludi.nl/blog/vbscript/active-directory-and-wmi-vbscript-to-enumerate-the-system-uptime-of-all-servers-in-active-directory-v2/
Best regards,
Dirk Adamsky – Deludi BV
What should I do if I get microsoft vbscript compilation error message when I run the vbscript for listing all servers in AD for uptime?