Scripting – Exchange mailbox size and number of messages

‘This script logs on to a server that is running Exchange Server and
‘displays the current number of bytes that are used in the user’s
‘mailbox and the number of messages.

‘ USAGE: cscript MailboxSize.vbs SERVERNAME MAILBOXNAME

‘ This requires that CDO 1.21 is installed on the computer.
‘ This script is provided AS IS. It is intended as a SAMPLE only.
‘ Microsoft offers no warranty or support for this script.
‘ Use at your own risk.

‘ Get command line arguments.
Dim obArgs
Dim cArgs

Set obArgs = WScript.Arguments
cArgs = obArgs.Count

Main

Sub Main()
Dim oSession
Dim oInfoStores
Dim oInfoStore
Dim StorageUsed
Dim NumMessages
Dim strProfileInfo
Dim sMsg

On Error Resume Next

If cArgs <> 2 Then
WScript.Echo “Usage: cscript MailboxSize.vbs SERVERNAME MAILBOXNAME”
Exit Sub
End If

‘Create Session object.
Set oSession = CreateObject(“MAPI.Session”)
if Err.Number <> 0 Then
sMsg = “Error creating MAPI.Session.”
sMsg = sMsg & “Make sure CDO 1.21 is installed. ”
sMsg = sMsg & Err.Number & ” ” & Err.Description
WScript.Echo sMsg
Exit Sub
End If

strProfileInfo = obArgs.Item(0) & vbLf & obArgs.Item(1)

‘Log on.
oSession.Logon , , False, True, , True, strProfileInfo
if Err.Number <> 0 Then
sMsg = “Error logging on: ”
sMsg = sMsg & Err.Number & ” ” & Err.Description
WScript.Echo sMsg
WScript.Echo “Server: ” & obArgs.Item(0)
WScript.Echo “Mailbox: ” & obArgs.Item(1)
Set oSession = Nothing
Exit Sub
End If

‘Grab the information stores.
Set oInfoStores = oSession.InfoStores
if Err.Number <> 0 Then

sMsg = “Error retrieving InfoStores Collection: ”
sMsg = sMsg & Err.Number & ” ” & Err.Description
WScript.Echo sMsg
WScript.Echo “Server: ” & obArgs.Item(0)
WScript.Echo “Mailbox: ” & obArgs.Item(1)
Set oInfoStores = Nothing
Set oSession = Nothing
Exit Sub
End If

‘Loop through information stores to find the user’s mailbox.
For Each oInfoStore In oInfoStores
If InStr(1, oInfoStore.Name, “Mailbox – “, 1) <> 0 Then
‘&HE080003 = PR_MESSAGE_SIZE
StorageUsed = oInfoStore.Fields(&HE080003)
if Err.Number <> 0 Then
sMsg = “Error retrieving PR_MESSAGE_SIZE: ”
sMsg = sMsg & Err.Number & ” ” & Err.Description
WScript.Echo sMsg
WScript.Echo “Server: ” & obArgs.Item(0)
WScript.Echo “Mailbox: ” & obArgs.Item(1)
Set oInfoStore = Nothing
Set oInfoStores = Nothing
Set oSession = Nothing
Exit Sub
End If

‘&H33020003 = PR_CONTENT_COUNT
NumMessages = oInfoStore.Fields(&H36020003)

if Err.Number <> 0 Then

sMsg = “Error Retrieving PR_CONTENT_COUNT: ”
sMsg = sMsg & Err.Number & ” ” & Err.Description
WScript.Echo sMsg
WScript.Echo “Server: ” & obArgs.Item(0)
WScript.Echo “Mailbox: ” & obArgs.Item(1)
Set oInfoStore = Nothing
Set oInfoStores = Nothing
Set oSession = Nothing
Exit Sub
End If

sMsg = “Storage Used in ” & oInfoStore.Name
sMsg = sMsg & ” (bytes): ” & StorageUsed
WScript.Echo sMsg
WScript.Echo “Number of Messages: ” & NumMessages
End If
Next

‘ Log off.
oSession.Logoff

‘ Clean up memory.
Set oInfoStore = Nothing
Set oInfoStores = Nothing
Set oSession = Nothing
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">