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:\temp\serveruptime.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 & "\root\cimv2")
	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

VN:F [1.9.3_1094]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)
Active Directory and WMI: VBscript to enumerate the system uptime of all servers in Active Directory, 10.0 out of 10 based on 1 rating
Like this Post? Consider sharing it on one of these:
  • Digg
  • LinkedIn
  • Slashdot
  • Technorati
  • RSS

6 Responses to “Active Directory and WMI: VBscript to enumerate the system uptime of all servers in Active Directory”

  1. [...] Active Directory and WMI: VBscript to enumerate the system uptime … [...]

  2. jamie says:

    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’

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  3. dirk adamsky says:

    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

    VN:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  4. Paul says:

    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

    VA:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.3_1094]
    Rating: 0 (from 0 votes)
  5. [...] 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 [...]

  6. dirk adamsky says:

    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

    VN:F [1.9.3_1094]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.3_1094]
    Rating: 0 (from 0 votes)

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam Protection by WP-SpamFree Plugin

© 2010 Dirk Adamsky Scripting Blog Suffusion WordPress theme by Sayontan Sinha