' Evan Hoffman ' April 2010 ' Script to generate a report of Exchange mailbox sizes including user's department and office location. ' evan.d.hoffman@gmail.com ' Original code from http://www.msexchange.org/articles/Scripting-Exchange-VBScript-ADSI-Part3.html Option Explicit 'On Error Resume Next Dim ServerList ' List of computers to check Dim server ' Current computer to check Dim fso ' File System Object Dim strWinMgmts ' Connection string for WMI Dim objWMIExchange ' Exchange Namespace WMI object Dim listExchange_Mailboxs ' ExchangeLogons collection Dim objExchange_Mailbox ' A single ExchangeLogon WMI object Dim logfile ' Output file Dim ldapUser ' LDAP user object from AD Const cWMINameSpace = "root/MicrosoftExchangeV2" Const cWMIInstance = "Exchange_Mailbox" Const LOG_FILE = "EMailSize.csv" '-------------------------------------- ' Set up the array of email servers '-------------------------------------- ServerList = Array("exchange1.site.com","exchange2.site.com") '-------------------------------------- ' Set up log file '-------------------------------------- set fso = CreateObject("Scripting.FileSystemObject") Dim date date = DateValue(Now) Set logfile = fso.CreateTextFile("MailboxSize-" & Year(date) & "-" & Month(date) & "-" & Day(date) & ".csv") 'WScript.Echo "Date: " & date & "." logfile.WriteLine("""Display Name"",""Department"",""Office"",""Mailbox Size (GB)"",""Mailbox Size"",""Mailbox Quota (KB)"",""Mailbox TotalItems"",""Mailbox StoreName"",""Mailbox ServerName""") ' Create the object string, indicating WMI (winmgmts), using the ' current user credentials (impersonationLevel=impersonate), ' on the computer specified in the constant cComputerName, and ' using the CIM namespace for the Exchange provider. WScript.Echo "Starting now" 'The rest of the script will fetch mailbox sizes for our servers. Mailbox sizes are in Kilobytes. For Each server in ServerList WScript.Echo "Starting " & server & " search." strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//" & server & "/" & cWMINameSpace 'WScript.Echo strWinMgmts Set objWMIExchange = GetObject(strWinMgmts) ' Verify we were able to correctly set the object. If Err.Number <> 0 Then WScript.Echo "ERROR: Unable to connect to the WMI namespace." Else 'The Resources that currently exist appear as a list of 'Exchange_Mailbox instances in the Exchange namespace. Set listExchange_Mailboxs = objWMIExchange.InstancesOf(cWMIInstance) ' Were any Exchange_Mailbox Instances returned? If (listExchange_Mailboxs.count > 0) Then ' If yes, do the following: ' Iterate through the list of Exchange_Mailbox objects. For Each objExchange_Mailbox in listExchange_Mailboxs Set ldapUser = Nothing Set ldapUser = LdapLookup( objExchange_Mailbox.MailboxDisplayName ) ' Display the value of the Size property. If Not (ldapUser Is Nothing) Then logfile.WriteLine("""" & objExchange_Mailbox.MailboxDisplayName & """,""" & ldapUser.department & """,""" & ldapUser.physicalDeliveryOfficeName & """,""" & (objExchange_Mailbox.Size / 1048576) & """,""" & objExchange_Mailbox.Size & """,""" & ldapUser.mDBOverQuotaLimit & """,""" & objExchange_Mailbox.TotalItems & """,""" & objExchange_Mailbox.StoreName & """,""" & objExchange_Mailbox.ServerName & """") Else logfile.WriteLine("""" & objExchange_Mailbox.MailboxDisplayName & """,,,""" & (objExchange_Mailbox.Size / 1048576) & """,""" & objExchange_Mailbox.Size & """,,""" & objExchange_Mailbox.TotalItems & """,""" & objExchange_Mailbox.StoreName & """,""" & objExchange_Mailbox.ServerName & """") End If Next Else ' If no Exchange_Mailbox instances were returned, display that. WScript.Echo "WARNING: No Exchange_Mailbox instances were returned." End If End If Next Wscript.Echo "Completed" Function LdapLookup(strDisplayName) Dim dc Dim userDN Dim ldapStr Dim objUser Dim conn Dim rs Dim FoundObject Set conn = CreateObject("ADODB.Connection") conn.Provider = "ADSDSOObject" conn.Open "ADs Provider" dc = "domaincontroller.site.com" ldapStr = ";(&(objectCategory=person)(displayName=" & strDisplayName &"));adspath;subtree" 'WScript.Echo ldapStr Set rs = conn.Execute(ldapStr) Set LdapLookup = Nothing If Not rs.EOF Then ' Set FoundObject = GetObject (rs.Fields(0).Value) Set LdapLookup = GetObject (rs.Fields(0).Value) ' WScript.Echo "Found user: " & FoundObject.cn &", Department: " & FoundObject.department ' Set LdapLookup = FoundObject rs.MoveNext End If End function