VBscript and WMI: VBscript to enumerate all mailboxes on a given Exchange server

Posted February 23rd, 2011 in exchange by dirk adamsky

This script is made for Jeff Doty.

What the script does:

  • make a wmi connection to a given exchange 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
  • replace the string ‘srv001′ with the name of your exchange server
  • save the script (for example c:tempmailboxes.vbs)
  • open a command prompt
  • go to “c:temp”
  • give “cscript mailboxes.vbs” (without quotes) and enter

When you want the output in a file please give this command:

“cscript mailboxes.vbs > mailboxes.txt” (again without the quotes)

The script:

' Name : mailboxes.vbs
' Description : script to enumerate all mailboxes on a given Exchange server
' Author : dirk adamsky - deludi bv
' Version : 1.10 (changed/corrected based on input by Mike)
' Date : 23-03-2011
' Level: intermediate

strServer = "srv001"
Const MinimalSize = 2048 'size in MB
Set objWMIExchange = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & strServer & "/root/MicrosoftExchangeV2")
Set colExchangeMailboxes = objWMIExchange.InstancesOf("Exchange_Mailbox")
For Each objExchangeMailbox in colExchangeMailboxes
    If (Left(objExchangeMailbox.StorageGroupName, 5) <> "Recov") And (Round(objExchangeMailbox.Size/1024,0) > MinimalSize) Then
		Wscript.Echo objExchangeMailbox.MailboxDisplayName & " ; " &_
			Round(objExchangeMailbox.Size/1024,0) & " MB"
	End If
Next
Set colExchange_Mailboxes = Nothing
Set objWMIExchange = Nothing

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

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

Active Directory: Openchange Technology preview

Posted June 9th, 2010 in exchange by dirk adamsky

Found out today that Julien Kerihuel and his team have a working preview of openchange server.
Can’t wait to do some CDO scripting against the openchange server……

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

Posted May 18th, 2010 in active directory, exchange, vbscript, wmi by dirk adamsky

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"]

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

Posted April 7th, 2010 in exchange 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