VBScript: Find All Exchange Servers in Active Directory

My friend Sharfa and I were exchanging some of our favorite code snippets and he showed me one for enumerating Exchange Servers in Active Directory. I dug the code but wanted to try to see if I could use my Recordset/ADsDSOObject skrills to shorten the code. The outcome isn’t any shorter but it does get the version, so that’s cool. Thanks, Sharfa, for pointing me towards the WMI Exchange_Server thing, too. :-D

Hit It

'****************************************************************************
' This script created by Chrissy LeMaire ([email protected])
' Website: http://netnerds.net/
'
' This script finds all Exchange Servers in AD. Includes Exchange Version.
'
' Run this script with admin privs on any computer within a domain.
'
' This script has only been tested on Windows Server 2003
'
' NO WARRANTIES, USE THIS AT YOUR OWN RISK, etc.
'*****************************************************************************

Set objAdRootDSE = GetObject("LDAP://RootDSE")
Set objRS = CreateObject("adodb.recordset")

varConfigNC = objAdRootDSE.Get("configurationNamingContext")

  strConnstring = "Provider=ADsDSOObject"
  strSQL = "SELECT * FROM 'LDAP://" & varConfigNC & "' WHERE objectCategory='msExchExchangeServer'"
  objRS.Open strSQL, strConnstring
    Do until objRS.eof
Set objServer = GetObject(objRS.Fields.Item(0))
   Call getExchangeInfo(objServer.CN)
Set objServer = Nothing
     objRS.movenext
    Loop
  objRS.close

Set objRS = Nothing
Set objAdRootDSE = Nothing

Sub getExchangeInfo(strServerName)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!" & strServerName & "\\ROOT\MicrosoftExchangeV2")
Set colItems = objWMIService.ExecQuery("Select * from Exchange_Server")

For Each objItem in colItems
MsgBox UCase(objItem.Name) & " (" & objItem.FQDN & ") is running Exchange " & objItem.ExchangeVersion
Next

Set colItems = Nothing
Set objWMIService = Nothing
End Sub

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, and holds a number of certifications, including those relating to SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in Active Directory, Exchange, VBScript
2 comments on “VBScript: Find All Exchange Servers in Active Directory
  1. Jeff Doty says:

    Hi Chrissy,

    Thanks for your work on other scripts, I have found them useful since I am not a scripting guru…more of a scripting theif. I couldn’t get this one to run. I is failing on the Set objWMIService line. There seems to be something wrong with the syntax, but I haven’t figured it out. What do you think?

    Jeff :-)

  2. Jason says:

    I was able to modify the script an run it with:

    Set objAdRootDSE = GetObject("LDAP://RootDSE")
    Set objRS = CreateObject("adodb.recordset")

    varConfigNC = objAdRootDSE.Get("configurationNamingContext")

    strConnstring = "Provider=ADsDSOObject"
    strSQL = "SELECT * FROM 'LDAP://" & varConfigNC & "' WHERE objectCategory='msExchExchangeServer'"
    objRS.Open strSQL, strConnstring
    Do until objRS.eof
    Set objServer = GetObject(objRS.Fields.Item(0))
    Call getExchangeInfo(objServer.CN)
    Set objServer = Nothing
    objRS.movenext
    Loop
    objRS.close

    Set objRS = Nothing
    Set objAdRootDSE = Nothing

    Sub getExchangeInfo(strServerName)
    Set objWMIService = GetObject("winmgmts:\" & strServerName & "ROOTMicrosoftExchangeV2")
    Set colItems = objWMIService.ExecQuery("Select * from Exchange_Server")

    For Each objItem in colItems
    MsgBox UCase(objItem.Name) & " (" & objItem.FQDN & ") is running Exchange " & objItem.ExchangeVersion
    Next

    Set colItems = Nothing
    Set objWMIService = Nothing
    End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *

*