Submitted By: Chris Swinford

Lists all the Microsoft Exchange servers in a forest.

Visual Basic
Edit|Remove
'this script will report all the database sizes for all exchange servers in your forest
'if the credentials specified below need secured please encode this script...
'run this in cscript.exe or plug in that little piece of code i see floating around that
'does that for you, or you will be clicking "OK" alot. :)

'will need these later
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

'use these credentials to authenticate to the exchange server
'NOTE: the admin shares on the exchange servers need to be turned on 
username = "user"
password = "password"
domain = "domain"

'what time is it?
wscript.echo "Report time: " & Now & VbCrLf

'this counter is used to increment the size of every database in the forest
exchforestsize = 0

'this will return me all the exchange servers in the forest using the EnumerateXchSvr function
arrExServerName = EnumerateXchSvr
For h = 0 To Ubound(arrExServerName)
   
   'for each exchange server in the array
   strExServerName = arrExServerName(h)
   'set the totalsgsize to 0 "as to reset it
   totalsgsize = 0
   'the hand off
   strserver = strExServerName
   'authenticate to the exchange server as the credentials specified above
   objshell.Run "%comspec% /c net use \\" & strserver & " /user:" & domain & "\" & username & " " & password & "",0,True
   
   'make sure nothing empty was returned from active directory 
   If len(strExServerName) > 0 Then
    	'send the exchange server name to the sgreport sub-routine
    	SGReport strExServerName
   End If
    
  'echo the size of all the exchange server's databases
    If len(strExServerName) > 0 Then 'make sure it is not a ghost record from AD
	 wscript.Echo "## Total Size of databases on Exchange server, " & strserver & ": " & int(totalsgsize) & " MB"
	 WScript.Echo "#################################################################################"
	End If
	
	'increment the size of all the exchange servers in the forest
	exchforestsize = int(totalsgsize) + int(exchforestsize)
totalsgsize = 0
Next

'echo the size of all exchange servers if it is not an AD ghost

 WScript.Echo VbCrLf
 WScript.Echo "#################################################################################"
 WScript.Echo "## Total size of all databases on all Exchange servers in the forest: " & Int(exchforestsize) & " MB"
 WScript.Echo "#################################################################################"
 WScript.Sleep 10000


Sub SGReport(strServer)

'all the players
Set iServer = CreateObject("CDOEXM.ExchangeServer")' the exchange server
Set iSGs = CreateObject("CDOEXM.StorageGroup")' the storage
Set iMBs = CreateObject("CDOEXM.MailboxStoreDB")' the mailbox store
Set iPUBs = CreateObject("CDOEXM.PublicStoreDB")' the public store

'open the server object 
iServer.DataSource.Open strServer
Version = iServer.ExchangeVersion
ServerType = iServer.ServerType

If ServerType = "1" Then
serverrole = "Frontend"
Else 
serverrole = "Bridgehead or Mailbox"
End If 

'return back the storage groups In an array
arrSGs=iServer.StorageGroups

WScript.Echo VbCrLf
WScript.Echo "#################################################################################"
wscript.Echo "## Exchange Server: " & strServer
WScript.Echo "##  Version: " & Version
WScript.Echo "##  Role: " & serverrole

'for all the storage groups in the array
For i=0 To UBound(arrSGs)
	strSGUrl=arrSGs(i)
	
	'open the storage group object
	iSGs.DataSource.Open "LDAP://" & iServer.DirectoryServer & "/" & strSGUrl
	
	
	
	If iSGs.CircularLogging = "True" Then
	loggingstyle = "Circular"
	Else
	loggingstyle = "Standard"
	End If
	
	
	arrlogpath = split(iSGs.LogFilePath, ":")
	logpath = "\\" & strserver & "\" & arrlogpath(0) & "$" & arrlogpath(1)
	
	WScript.Echo "##    Storage Group: " & iSGs.Name
	wscript.Echo "##     Logging Style: " & loggingstyle
	
	
	set objlogfolder = objFSO.GetFolder(logpath)
	Set arrlogfolder = objlogfolder.Files
	
	totallogsize = 0
	
	For Each logfile In arrlogfolder
	 logsize = 0
	 If InStr(logfile, ".log") Then
	  Set objFileLog = objFSO.GetFile(logfile)
      logsize = Int(left(objFilelog.Size/1048576, 8))
      totallogsize = Int(totallogsize) + Int(logsize)
	 End If
	Next
	
	WScript.Echo "##     Total size of Logfiles for storage group " & iSGs.Name & ": " & totallogsize & " MB"
	totallogsize = 0
	
	'this counter will be used for the total size of all databases on a exchange server
	totalcounter = 0
	
	'give me the public stores in an array
	arrpubStores = iSGs.PublicStoreDBs 
	
	'give me the private stores in an array
	arrMBStores = iSGs.MailboxStoreDBs
	
	'for each private store
	For j = 0 To UBound(arrMBStores)
		
		'open the database object
		iMBS.DataSource.open "LDAP://" & arrMBStores(j)	
		
		'we use this to turn the local path into a UNC path
        arrDBPath = Split(iMBS.DBPath, ":")
        
        'here we do a little string manipulation to turn the local path into a UNC path
        DBunclocation = "\\" & strserver & "\" & arrdbpath(0) & "$" & arrdbpath(1)
        
        'lets get the STM file location ' here im assuming you STM files have the same name as your EDB
        'files, If they aren't god bless you
         STMunclocation = Replace(DBunclocation, ".edb", ".stm")
       
       'get the file information on these puppies
       Set objFileDB = objFSO.GetFile(DBunclocation)
       Set objFileSTM = objFSO.GetFile(STMunclocation)
        
        'how big are you guys
        stmsize = int(left(objFileSTM.Size/1048576, 8))
        dbsize = int(left(objFileDB.Size/1048576, 8))
        	
	    'inform the user on their size
	    Wscript.Echo "##       Total size for mailbox database, " & iMBS.Name & ": " & int(dbsize) + int(stmsize) & " MB"
        	
	    'increment our counter with the sizes
	    totalcounter = int(totalcounter) + int(dbsize) + int(stmsize)
	
	Next 'move on until none are left
   
   
   
   'if our array of public stores is not empty
   If IsNull(arrpubStores) = False Then
    'then for every public store
    For p = 0 To UBound(arrpubStores)
		
		'open the database object
		iPUBs.DataSource.open "LDAP://" & arrpubStores(p)	
		
		'we use this to turn the local path into a UNC path
        arrDBPath = Split(iPUBs.DBPath, ":")
        
        'here we do a little string manipulation to turn the local path into a UNC path
        DBunclocation = "\\" & strserver & "\" & arrdbpath(0) & "$" & arrdbpath(1)
        
        'lets get the STM file location ' here im assuming you STM files have the same name as your EDB
        'files, If they aren't god bless you
        STMunclocation = Replace(DBunclocation, ".edb", ".stm")
       
       'get the file information on these puppies
       Set objFileDB = objFSO.GetFile(DBunclocation)
       Set objFileSTM = objFSO.GetFile(STMunclocation)
        
        'how big are you guys
        stmsize = int(left(objFileSTM.Size/1048576, 8))
        dbsize = int(left(objFileDB.Size/1048576, 8))
        	
	    'inform the user on their size
	    Wscript.Echo "##       Total size for public database, " & iMBS.Name & ": " & int(dbsize) + int(stmsize) & " MB"
        
        'increment our counter with the sizes
	    totalcounter = int(totalcounter) + int(dbsize) + int(stmsize)
	
	Next 'move on until none are left
   
   'set the totalsgsize to 0 "as to reset it
   
   
   End If 'end the if isnull(arrpubstores)

'tell the user the total size of the storage group
WScript.Echo "##    Total Size for storage group, " & iSGs.Name & ": " & int(totalcounter) & " MB"
totalsgsize = int(totalcounter) + int(totalsgsize)


Next 'move on to the next storage group





End Sub



Function EnumerateXchSvr 'returns me a list of all the exchange servers in the forest in an Array

'standard ADO, ADSI stuff
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
strConfigurationNC = objRootDSE.Get("configurationNamingContext")
strSchemaNC = objRootDSE.Get("schemaNamingContext")
strExchangeCN = "LDAP://cn=Microsoft Exchange,cn=Services" & "," & strConfigurationNC
strCMDTxt = "Select distinguishedName, name from '" & strExchangeCN & " 'where objectCategory='CN=ms-Exch-Exchange-Server," & strSchemaNC & "'"
objCommand.CommandText = strCMDTxt
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30 
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

Set objRecordSet = objCommand.Execute
If objRecordSet.RecordCount > 0 Then
	objRecordSet.MoveFirst
Else
	WScript.Echo "no records found"
	WScript.Quit
End If
ReDim arrXchSvr(0)
k = 0

Do Until objRecordSet.EOF 'build our array
	strXchSvr = objRecordSet.Fields("Name").Value
    arrXchSvr(k) = strXchSvr
    k = k + 1 
    redim preserve arrXchSvr(k)
    objRecordSet.MoveNext
Loop

'and here ya go
EnumerateXchSvr = arrXchSvr
End Function