Thursday, August 16, 2007

Mail-Box Count Script

Below is a nice script to run against your exchange server and get the count of mailboxes, users and size of the mailboxes. You don't have to know scripting at all to make this work. Follow the steps I am listing below, of course I got it from a coworker.

  • Copy and paste the entire code into notepad
  • Change the server name, into your server name (ServerList = Array("server1", "server2", "server3")
  • Save the file on your Hard drive as mailboxCount.vbs ( c:\ mailboxCount.vbs)
  • Now go to command line
  • Drill down to same directory ( c:\ mailboxCount.vbs)
  • Run the file by typing, c:\ mailboxCount.vbs


 

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

Const cWMINameSpace = "root/MicrosoftExchangeV2"

Const cWMIInstance = "Exchange_Mailbox"

Const LOG_FILE = "EMailSize.csv"

'--------------------------------------

' Set up the array of email servers

'--------------------------------------

ServerList = Array("server1", "server2", "server3")

'--------------------------------------

' Set up log file

'--------------------------------------

set fso = CreateObject("Scripting.FileSystemObject")

Set logfile = fso.CreateTextFile(LOG_FILE)

logfile.WriteLine("""Display Name"",""Mailbox Size"",""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

                ' Display the value of the Size property.

                logfile.WriteLine("""" & objExchange_Mailbox.MailboxDisplayName & """,""" & objExchange_Mailbox.Size & """,""" & objExchange_Mailbox.TotalItems & """,""" & objExchange_Mailbox.StoreName & """,""" & objExchange_Mailbox.ServerName & """")

            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"


 

Best Regards

Oz Ozugurlu

20 comments:

Anonymous said...

THANK YOU!
Works like a charm. Just what I was looking for.

Oz Casey, Dedeal said...

I am glad it did, thanks for leaving comments

Cheers
oz

Anonymous said...

Nice work!

Anonymous said...

I have been lokking for something like this for a month, mostly over complicated but you script was on the mark 100%.. thank you.

I dont suppose you have one that can do the same thing with AD users?

Anonymous said...

This script is very useful and precise to the point .
Gr8 Work . You rock .

Anonymous said...

Hi.
That is cool.
Can you help me with the script to Count the mail traffic on exchange Server 2003 and 2007.

To count
Mail received and mail sent out on daily basis.
I will be glued to your blog to see if you can offer me the soln.

My email is eliudkat2@yahoo.co.uk .Thanks

Anonymous said...

Great script, saved me alot of time.

Anonymous said...

Worked a charm for my Exchange 2003 server. After some digging I found you can't use the WMI stuff on Exchange 2007 - which is a shame because this info is exactly what I wanted.

Anonymous said...

Friggin awesome! used this with a pivottable in excel and got a nice table telling me user count per server per database. thank you so much!

Anonymous said...

I am getting the following error, even though I am running this directly on the Exchange 2003 box. (I also replaced the serverlist with my server.)

"WARNING: No Exchange_Mailbox instances were returned."

Any ideas?

Anonymous said...

That is fantastic. Thanks so much for posting.

Anonymous said...

Great script! Thanks for sharing.

Oz Casey, Dedeal said...

Glad you liked it
Thanks for the feedback
oz

Unknown said...

Did not work for me. ERROR: Unable to connect to the WMI namespace.

Unknown said...

@Sinner: I'm getting the same error. Any luck?

Unknown said...

thanks...

Anonymous said...

Thanks, Oz. Just what I needed.

Jesse said...

@Sinner & Carson I know this is an old post now, but I am receiving the same error you two were. Any help on how you resolved it would be great.

Thanks,
Jesse

Anonymous said...

will this work for exchage 2010?

ADV Movers said...

Best Moving Company +971 50 3605353

We are Cheap Movers in Dubai, providing best moving

services at reasonable prices. Call Now for Residential and Commercial Dubai Movers +971503605353

Dubai Movers
Movers Dubai
moving services dubai
moving company
movers dubai
home movers dubai
Packer in Dubai
Local Movers
Cheap Movers in Dubai
Dubai Local Movers

http://www.advmovers.com/