6

Active Directory and WMI: VBscript to show the size of a specific mailbox

Today I made a script as requested by Jamal.
It is a further development of a script to enumerate all exchange mailboxes and their size which can be found here.

What the script does:

  • ask for the smtp address of the mailbox
  • get the displayname and homembd properties of that mailbox
  • enumerate all exchange servers
  • make a wmi connection with the exchange server on which the mailbox resides
  • get the size of the mailbox

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:tempmailboxsize.vbs)
  • open a command prompt
  • go to “c:temp”
  • give “cscript mailboxsize.vbs” (without quotes) and enter

The script:

' Name : mailboxsize.vbs
' Description : script to show the size of a specific mailbox
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 18-05-2010
' Level: advanced

Dim strDisplayName, strHomeMDB
strSMTP = InputBox("Please fill in the SMTP address of the user")
GetDisplayNameAndHomeMDB(strSMTP)

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
	If Instr(strHomeMDB,adoRecordset.Fields("name").Value) > 1 Then
		Set objWMIExchange = GetObject("winmgmts:{impersonationLevel=impersonate}!//"&_
		adoRecordset.Fields("name").Value & "/root/MicrosoftExchangeV2")
		Set colExchangeMailboxes = objWMIExchange.ExecQuery("Select * From Exchange_Mailbox Where MailboxDisplayName = '" & strDisplayName & "'")
		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
	End If
	adoRecordset.MoveNext
Loop

adoRecordset.Close
adoConnection.Close

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

Function GetDisplayNameAndHomeMDB(strMail)
	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")
	strDNSDomain = objRootDSE.Get("defaultNamingContext")
	strBase = "<LDAP://" & strDNSDomain & ">"
	strFilter = "(&(objectCategory=person)(objectClass=user)(mail=" &  strMail & "))"
	strAttributes = "displayName,homeMDB"

	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
	strDisplayName = adoRecordset.Fields("displayName").Value
	strHomeMDB = adoRecordset.Fields("homeMDB").Value
	adoRecordset.Close
	adoConnection.Close

	Set adoRecordset = Nothing
	Set objRootDSE = Nothing
	Set adoConnection = Nothing
	Set adoCommand = Nothing
End Function

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

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

[adrotate group="2"]


Related Posts:
  • Active Directory and WMI: VBscript to enumerate a sorted list of all mailboxes and their size in your AD domain
  • Active Directory: VBscript to enumerate the Outlook details of all users in your company
  • Active Directory: VBscript to enumerate all contacts in your domain

  • 6 Responses so far.

    1. jamal aiouani says:

      hi Dirk,

      sorry for not answering sooner, as i had to test the script on multiple devices.

      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 it be possible to add more output data size as :

      Outbox
      Sent Items
      Deleted Items
      Calendar

      Many Thanks
      JAMAL

    2. dirk adamsky says:

      Hi Jamal,

      The getonject error is a result of insufficient rights on the target (exchange server) machine.
      You need admin rights to do the wmi queries.
      With the above script it is not possible to do a rundown of the subfolders of a mailbox.
      WMI only gives a certain set of mailbox properties.
      For what you want to achieve you need CDO (Collaborative Data Objects).
      I can make a script for you but that will be next week or so.

      Best regards,

      Dirk Adamsky

    3. kasi says:

      Hi..

      I have a developed a connector plugin using c# on exchange server 2007,Its working fine,Now I tried
      to work with exchange server 2003 ,Here I am facing an error “No Window power shell snap-in nor found for
      version 1″,Now I want alternative programmatic solution for window power shell snap-in,

      Any Help Greatly Appreciated,

      Thanks & Regards

      kasi

    4. dirk adamsky says:

      Hi Kasi,

      Have you already installed powershell on the exchange 2003 server?

      Best regards,

      Dirk Adamsky

    5. Chris Hightower says:

      Dirk,

      This is the best example I’ve found of searching for a particular mailbox! I have a problem though, I need to generate a list of all AD users with an active mailbox along with some attributes from the AD side and other attributes from exchange for a migration. Is there a way I can get both simultaneously in one dump?

      Thanks,

      Chris Hightower

    6. dirk adamsky says:

      Hi Chris,

      sorry for my late reply.
      I did make a script that enumerates all Exchange mailboxes.
      It can be found here:

      http://deludi.nl/blog/vbscript/active-directory/exchange-active-directory-vbscript/active-directory-and-wmi-vbscript-to-enumerate-all-mailboxes-and-their-size-of-your-ad-domain/.

      Best regards,

      Dirk Adamsky

    Leave a Reply