A Script for Getting Mailbox Sizes (using WMI)

Originally published July 26, 2004


In this post I provided a script for getting mailbox sizes. One of the questions posed by a reader was:

These methods create a MAPI connection to each mailbox thereby changing the last login information on each mailbox, correct? Does anyone know a way to enumerate the mailboxes on a server without changing the last login info? (like ESM does)”

And the writer was, of course, correct. Today, I'm giving a script that, for Exchange 2003 servers, uses WMI (Windows Management Instrumentation) to provide the same information. It doesn't update the last login information.

The downside? The WMI solution requires Exchange 2003. The earlier solution works in a mixed Exchange 2000 and 2003 environment, and could easily be modified to work with Exchange 5.5, since it uses MAPI. The WMI solution also makes it more difficult to provide OU-based information (which is important in my environment, and probably in most large environments, but not in small or medium-sized shops).

It's a toss-up. There are a number of technologies available for querying and modifying Exchange: WMI, CDO, CDOEX, CDOEXM, ADSI, MAPI, Webdav, and probably others. It can sometimes be difficult to choose which one is best for you...

' based on http://msdn.microsoft.com/library/default.asp?url=/library/en-us/e2k3/e2k3/_wmiref_cl_Exchange_Mailbox.asp
' works only with Exchange 2003 servers

' Purpose:   Display each Exchange_Mailbox found for Exchange server,
'            and show selected, formated properties on the
'            Exchange_Mailbox objects
' Change:    cComputerName [string] the computer to access (netbios name)
' Output:    Displays the name of each Exchange_Mailbox and properties
Option Explicit

On Error Resume Next

' the netbios name of the server whose mailboxes you want to return
Dim cComputerName : cComputerName = "orange"

Const cWMINameSpace = "root/MicrosoftExchangeV2"
Const cWMIInstance = "Exchange_Mailbox"

Dim strWinMgmts                 ' Connection string for WMI
Dim objWMIExchange              ' Exchange Namespace WMI object
Dim listExchange_Mailboxes       ' Exchange_Mailbox collection
Dim objExchange_Mailbox         ' A single Exchange_Mailbox WMI object

 ' 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.
 strWinMgmts = "winmgmts:{impersonationLevel=impersonate}!//" & _
  cComputerName & "/" & cWMINameSpace
 Set objWMIExchange =  GetObject (strWinMgmts)

 ' Verify we were able to correctly connect to the WMI namesspace on the server
 If Err.Number <> 0 Then
  WScript.Echo "ERROR: Unable to connect to the WMI namespace."
  WScript.Echo err.description & " (" & Hex (err.number) & ")"

  WScript.Quit 1
 End If

 ' The Resources that currently exist appear as a list of
 ' Exchange_Mailbox instances in the Exchange namespace.
 Set listExchange_Mailboxes = objWMIExchange.InstancesOf (cWMIInstance)

 ' Were any Exchange_Mailbox Instances returned?
 If (listExchange_Mailboxes.count <= 0) Then
  set objWMIExchange = Nothing
  WScript.Echo "WARNING: No Exchange_Mailbox instances were returned."
  WScript.Quit 1
 End If

 ' Iterate through the list of Exchange_Mailbox objects.
 For Each objExchange_Mailbox in listExchange_Mailboxes
  Dim strTmp, strOut

  strTmp = objExchange_Mailbox.LegacyDN
  strTmp = Right (strTmp, Len (strTmp) - InStrRev (strTmp, "CN="))
  strTmp = Mid (strTmp, 3)

  strOut = objExchange_Mailbox.MailboxDisplayName & " (" & strTmp & ") "
  If Len (strOut) > 50 Then
   strOut = Left (strOut, 50)
  End If

  If Len (strOut) < 50 Then
   strOut = strOut & Space (50 - Len (strOut))
  End If

  strTmp = FormatNumber (objExchange_Mailbox.Size, 0) & " KB "

  If Len (strTmp) < 14 Then
   strTmp = Space (14 - Len (strTmp)) & strTmp
  End If

  strOut = strOut & strTmp

  strTmp = FormatNumber (objExchange_Mailbox.TotalItems, 0)

  If Len (strTmp) < 8 Then
   strTmp = Space (8 - Len (strTmp)) & strTmp
  End If

  strOUt = strOut & strTmp

  WScript.Echo strOut

 set objWMIExchange = Nothing

Published Tuesday, November 13, 2007 6:01 PM by michael
Filed under: , ,


Tuesday, November 13, 2007 9:02 PM by Michael's meanderings...

# A Script for Getting Mailbox Sizes (using WMI in PowerShell)

Originally published June 28, 2007 This post and the script is Exchange 2003 friendly. As long as you

Sunday, January 18, 2009 10:00 AM by Need help on Exchange recipient Policy !! | keyongtech

# Need help on Exchange recipient Policy !! | keyongtech

Pingback from  Need help on Exchange recipient Policy !! | keyongtech