43

Active Directory and WMI: VBscript to enumerate all mailboxes and their size in your AD domain

Posted April 7th, 2010 in exchange and tagged , , , , , , , , , by dirk adamsky

Today’s script is an extension of yesterday’s script.
What the script does:

  • get all exchange servers from your AD domain
  • make a wmi connection to each server and create a list of the mailboxes and their size

The script is tested in an win2003/exchange2003 environment.

Follow the next steps to run the script (admin rights needed):

  • copy and paste the script in your favorite text editor
  • save the script (for example c:\temp\listallmailboxes.vbs)
  • open a command prompt
  • go to “c:\temp”
  • give “cscript listallmailboxes.vbs” (without quotes) and enter

The script:

' Name : listallmailboxes.vbs
' Description : script to enumerate all mailboxes and their size in your AD domain
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 07-04-2010
' Level: intermediate

Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

Set objRootDSE = GetObject("LDAP://RootDSE")
strBase = "<LDAP://" & objRootDSE.Get("configurationnamingcontext") & ">"
strFilter = "(objectCategory=msExchExchangeServer)"
strAttributes = "name"

strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

Set adoRecordset = adoCommand.Execute

Do Until adoRecordset.EOF
	Set objWMIExchange = GetObject("winmgmts:{impersonationLevel=impersonate}!//"&_
	adoRecordset.Fields("name").Value & "/root/MicrosoftExchangeV2")
    Set colExchangeMailboxes = objWMIExchange.InstancesOf("Exchange_Mailbox")
    For Each objExchangeMailbox in colExchangeMailboxes
        If Left(objExchangeMailbox.StorageGroupName, 5) <> "Recov" Then
			Wscript.Echo adoRecordset.Fields("name").Value & " ; " & objExchangeMailbox.MailboxDisplayName & " ; " &_
			Round(objExchangeMailbox.Size/1024,0) & " MB"
		End If
	Next
	Set colExchange_Mailboxes = Nothing
	Set objWMIExchange = Nothing
	adoRecordset.MoveNext
Loop

adoRecordset.Close
adoConnection.Close

Set adoRecordset = Nothing
Set objRootDSE = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing

When you have problems/questions please post a reply or give a ‘star’ rating.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV


Related Posts:
  • Active Directory: VBscript to count users with multiple entries in a nested distribution group
  • Active Directory and WMI: VBscript to enumerate a sorted list of all mailboxes and their size in your AD domain
  • Active Directory And WMI: VBscript to monitor all servers in Active Directory

  • 43 Responses so far.

    1. Verdure says:

      Dirk,

      Thanks for your script.
      I would like to use your script for a specific exchange server and a specific OU.
      Hoe can i parameter it?

      Thanks in advance,

      Kind regards,
      Verdure

    2. dirk adamsky says:

      Hi Verdure,

      You can do that by changing the ADO filter line.
      For example when the servername is srv001 =>

      strFilter = “(&(objectCategory=msExchExchangeServer)(cn=srv001))”

    3. dirk adamsky says:

      For the OU: you can change the value of strBase.
      For example:

      strBase = "<LDAP://ou=servers,dc=test,dc=org>"
      

      Best regards,

      dirk adamsky

    4. jamal aiouani says:

      Hi Dirk,

      It is a very efficient script,

      I was wondering if there is a way to add in an input for a user ID or Email address on a Domain

      To get the result for the one user only

      Thank you

      JAMAL

      • dirk adamsky says:

        Hi Jamal,

        This particular script is not very practical to get the mailbox size of a single user.
        I will make a new script for you next monday.

        Best regards,

        Dirk Adamsky

    5. jamal aiouani says:

      Thank you very much Dirk,

      I really appreciate your help please let me when you have it ready

      Many Thanks

      JAMAL

    6. dirk adamsky says:

      Hi Jamal,

      I have made an extension to the script.
      It can be found here:

      http://deludi.nl/blog/vbscript/active-directory/exchange-active-directory-vbscript/active-directory-and-wmi-vbscript-to-show-the-size-of-a-specific-mailbox/

      Please let me know if it works for you.

      Best regards,

      Dirk Adamsky

    7. jamal aiouani says:

      hi Dirk,

      it works perfect; only for one issue you need to run it under an Admin account if not you get the error below:

      “permission denied ‘GetObject’ ”

      is there a way to avoid this error by allowing everyone to use the script within the domain

      Another request;

      would be available to add more output data size as :

      Outbox
      Sent Items
      Deleted Items
      Calendar

      Many Thanks
      JAMAL

    8. Eelko says:

      Nice, perfect. Two thumbs up in a circle.

    9. Duwane Brwon says:

      Hello! I need the script to running on exchange 2003 , server 2003 and provide users names and office information (attributes of AD) for each user….can that be done? Thank you in advance!

    10. dirk adamsky says:

      Hi Duwane,

      I will extend the script for you next monday.

      Best regards,

      Dirk Adamsky

    11. Duwane Brwon says:

      Thanks! I have be able to query all mailboxes on all servers and provided sizes and be able to report if they are over their limit and have much by and user information (office, billing code, organization)….THANKS!

    12. Duwane Brwon says:

      Thanks again on editing the script…just let me know when

    13. Copper says:

      Is it possible to extend the script to obtain the exchange version (2000/2003/200… and profession/ standard)?
      Thanks in advance,

    14. dirk adamsky says:

      Hi Copper,

      It is certainly possible but in general you, as an administrator, know what Exchange version is used.
      I have written the script for a Win 2003 and Exchange 2003 environment. It is not tested for 2000 or 2007 environments.

      Best regards,

      Dirk Adamsky

    15. Verdure says:

      Hi Dirk,

      Could you change your “VBscript”to enumerate all mailNickname – DispalyName – mailboxes and their size in your AD domain

      Thanks in advance,

      Verdural

    16. corn says:

      Hi Dirk,

      i tried to lock this script to one OU, but when i fill in the ldap Path of that OU the script stops with the error that the AD Path ist wrong. can you give me an example for the correct syntax?

      i tried
      strBase = “” & objRootDSE.Get(“configurationnamingcontext”) & “>”

      this path is correct but not found.

      regards and thanks for your good work :)

    17. dirk adamsky says:

      Hi Corn,

      Sorry for my late reply.
      To run the script on a specific OU please replace these 2 lines:

      Set objRootDSE = GetObject("LDAP://RootDSE")
      strBase = "<LDAP://" & objRootDSE.Get("configurationnamingcontext") & ">"
      

      with

      Set objRootDSE = GetObject(“LDAP://ou=mailserver,dc=test,dc=org”)

      Best regards,

      Dirk Adamsky

    18. Jeff Doty says:

      Hi Dirk,

      I would really like to use your script and I made the mod to only point to one server, but I am not getting anything. I also moded it to write the output to a file.

      Thanks for the script, can you help with my problem?

    19. corn says:

      Hi,
      Sorry i dont get it :(

      i replaced the lines as you said. but when i try to run the script, it comes to an error an line 24,1 (set adoRecordset)

      regards
      corn

    20. dirk adamsky says:

      Hi corn,

      sorry for my late reply.
      Can you please post the contents of your line that starts with ‘Set objRootDSE =’.

      Best regards,

      Dirk Adamsky

    21. dirk adamsky says:

      Hi Jeff Doty,

      Sorry for my late reply.
      I decided to make a script for you.
      It can be found here:

      http://deludi.nl/blog/vbscript/active-directory/exchange-active-directory-vbscript/vbscript-and-wmi-vbscript-to-enumerate-all-mailboxes-on-a-given-exchange-server/

      Hope this is what you want.

      Best regards,

      Dirk Adamsky

    22. Gavin says:

      Hi, thanks for the script, can the output be sorted by mailbox size?

    23. Gavin says:

      Hi Dirk,

      Thanks for the suggestion but I have used the script to auto email to me as my Mailbox store is always hitting it’s max of 75 Gb, it would be nice if I didn’t have to manually edit the data each day.

      Thanks

    24. dirk adamsky says:

      Hi Gavin,

      I will write a script for you.
      Please give me a couple of days.

      Best regards,

      dirk adamsky

    25. Gavin says:

      Many thanks Dirk

    26. dirk adamsky says:

      Hi Gavin,

      I did some research: vbscript does not have methods for sorting arrays.
      I have to use a bubble sort function to do the sorting.
      This is a somewhat slow and cpu intensive option.
      There is an escape: when you have a .net runtime on the machine where the script runs you can use an Arraylist (System.Collections.Arraylist).
      The Arraylist object has a sort funtion.
      Now the golden question: do you have .net on your script server or are you able to install it/

      Best regards,

      dirk adamsky

    27. Gavin says:

      Hi Dirk,

      Sorry for delay, been on vacation. Yes we do have .NET on the server.

      Thanks

      Gavin

    28. Tom S says:

      Dirk,

      Boy, you sure are doing a lot of work for folks. You are a nice guy on top of being a hell of a scripter. I was wondering how I might use your script to dump the output to a .CSV so that it can be analyzed in excel. Any help would be greatly appreciated. I really like the simple output of your original script but it’s coming up in individual message boxes 1 by 1 and I have to click ok over and over (or kill WSH all together). Just want the output dumped to a file and don’t know how to go about it.

      Thanks!!!

      Tom

    29. Tom S says:

      Dirk..

      I’m an idiot. Don’t waste your time responding. I just executed at the command line and piped the output into a file. Management has made me rusty. My apologies for cluttering your blog.

      Tom

    30. Mohamed Imran says:

      I’m not sure what I’m doing wrong. I hope you can help.

      I’m running command prompt as administrator and when I type:

      C:\temp>cscript.exe templistallmailboxes.vbs

      I get the following:

      Microsoft (R) Windows Script Host Version 5.8
      Copyright (C) Microsoft Corporation. All rights reserved.

      C:\temp\templistallmailboxes.vbs(28, 2) Microsoft VBScript runtime error: Permission denied: ‘GetObject’

      What am I doing wrong?

      Hope you can Help,

      Mohamed Imran

    31. Mohamed Imran says:

      Okay, I figured it out – the script works fine when run directly from the server.

      Thank you.

      Mohamed Imran

    32. Dave-G says:

      Thanks for this script Dirk, it does exactly what i need!

    33. leftygardner says:

      This is amazing, but is there a way we could include displaying the users outlook details as well, e.g. company?

    34. Chris says:

      Hi Dirk,
      I am trying to use this script and unfortunately am getting errors. I am running it from a 2003 exchange server. When I run the script exactly as it is here I get an error:
      “Microsoft VBScript runtime error: The remote server machine does not exist or is unavailable: ‘GetObject’”

      When I try and run it just on one OU (which is really what I need to do) I get the error:
      “Provider: Table does not exist”

      Here are the two lines I saw you requested from a poster earlier:
      Set objRootDSE = GetObject(“LDAP://RootDSE”)
      strBase = “”

    35. Chris says:

      Not sure why it didnt show up… the strBase line I am using is:
      strBase = “”

    36. dirk adamsky says:

      Hi Chris,

      Sorry for my late reply (was very busy with other projects).
      The line for strBase should be:

      strBase = "<LDAP://" & objRootDSE.Get("configurationnamingcontext") & ">"

      This stands for your local domain.

      I had to use the WordPress plugin for publishing sourcecode…
      In the regular comment field the line is shown like strBase = “”.
      Everything between the quotes diappears…..

      Happy scripting.

      Best regards,

      Dirk Adamsky

    Leave a Reply