Submitted By: Michael Cook

Adds a replica server to the replica list of all of the public folders on a Microsoft Exchange 2003 server.

Visual Basic
Edit|Remove
Const strWMINameSpace = "root/MicrosoftExchangeV2"
Const strWMIInstance = "Exchange_PublicFolder"

' The DN for the public folder store of the new server, best found via WMI or ADSIEDIT 
' Configuration, Services, Microsoft Exchange, Domain, Administrative Groups, Organization, 
' Servers, Server, Information Store, DN 

Const NEWREPLICASERVER = "cn=Public Store,cn=Storage Group,cn=InformationStore,cn=mail,cn=Servers,cn=contoso," & _
        "cn=Administrative Groups,cn=contoso,cn=Microsoft Exchange,cn=Services,cn=Configuration,dc=contoso,dc=com"

strComputer = "oldmailserver"

Dim strWinMgmts  
' Connection string for WMI 
Dim objWMIExchange 
' Exchange Namespace WMI object 
Dim colPublicFolders 
' ExchangeLogons collection 
Dim objWMIPublicFolder  
' A single ExchangeLogon WMI object

' Create the object string, indicating WMI (winmgmts), using the 
' current user credentials (impersonationLevel=impersonate), 
' on the computer specified in the constant strComputer, and 
' using the CIM namespace for the ExchangeClusterResource provider.

strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//" & strComputer & "/" & strWMINameSpace

Set objWMIExchange =  GetObject(strWinMgmts)

' The Resources that currently exist appear as a list of ExchangeLogon instances in the Exchange namespace.
Set colPublicFolders = objWMIExchange.InstancesOf(strWMIInstance)

' Were any Exchange_PublicFolder Instances returned?
Wscript.Echo "Number of Public Folders returned: " & colPublicFolders.Count 
If (colPublicFolders.count > 0) Then 
'  If yes, iterate through the list of Exchange_PublicFolder objects.
 For Each objWMIPublicFolder in colPublicFolders
'   Return the address book name of the public folder instance
  Wscript.Echo"Address Book Name: " & objWMIPublicFolder.AddressBookName
'   Check to see if the public folder instance has a replica on the new server
'   If it doesn't have a replica on the new server, create a replica on the new server
  If CheckReplica(objWMIPublicFolder) = TRUE Then
   Wscript.Echo VBTAB & "Replica on NEWREPLICASERVER"
  Else
   objWMIPublicFolder.AddReplica NEWREPLICASERVER
   Wscript.Echo VBTAB & "Replica added pointing to NEWREPLICASERVER"
  End If

 Next
Else
'  If no Exchange_PublicFolder instances were returned, display that.
 WScript.Echo "WARNING: No Exchange_PublicFolder instances were returned."
End If

Function CheckReplica(objWMIPublicFolder) 
'  Create a "switch" to flip if the new server DN is in the replica list  
bolNEWREPLICASERVERCheck = FALSE 
'  Loop through the replica list and compare to the DN of the new server 
'  If there is a match found, flip the "switch"
 For Each strReplica in objWMIPublicFolder.ReplicaList
  If strReplica = NEWREPLICASERVER Then
   bolNEWREPLICASERVERCheck = True
  End IF
 Next
'  Return the results of the check as TRUE or FALSE  
CheckReplica = bolNEWREPLICASERVERCheck 
End Function