VbScript: GetStores

A VbScript function to retrieve all Exchange Information Stores from Active Directory, returning the results as a Dictionary object containing the store name and store distinguishedName.

Function GetStores
    ' Return Type: Scripting.Dictionary
    '
    ' Finds all Private Information Stores in AD
    
    Const ADS_SCOPE_SUBTREE = 2
    
    Dim objConnection : Set objConnection = CreateObject("ADODB.Connection") 
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    
    Dim objCommand : Set objCommand = CreateObject("ADODB.Command")
    objCommand.ActiveConnection = objConnection
    
    Dim objRootDSE : Set objRootDSE = GetObject("LDAP://RootDSE")
    
    objCommand.CommandText = "SELECT name, distinguishedName " & _
        "FROM 'LDAP://" & objRootDSE.Get("configurationNamingContext") & _
        "' WHERE objectClass='msExchPrivateMDB'"
        
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Timeout") = 600 
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
    objCommand.Properties("Cache Results") = False
    
    Dim objRecordSet : Set objRecordSet = objCommand.Execute
    Dim objStores : Set objStores = CreateObject("Scripting.Dictionary")
    
    While Not objRecordSet.EOF
        Dim strDN : strDN = objRecordSet.Fields("distinguishedName").Value 
        Dim strName : strName = objRecordSet.Fields("name").Value
        
        objStores.Add strDN, strName
        objRecordSet.MoveNext
    WEnd
    Set GetStores = objStores
End Function

Usage example

Set objStores = GetStores
For Each strDN in objStores
    WScript.Echo "DN: " & strDN & vbCrLf & "Name: " & objStores(strDN)
Next