#149875 - 2005-10-13 02:56 PM
Getting mailbox size and number of items from exchange.
|
Mart
KiX Supporter
Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
|
Folks,
Trying to get the users mailbox size and number of items in the mailbox from exchange. Found the code below at MS How to programmatically get the size of mailboxes in Exchange. Works great but it’s in VBS. Tried to convert it but failed cause my VB knowledge is to little.
Wonder if anybody here is willing to translate so I can get the same results as I get from the VBS script but then from kix based script. Did a search here if this or something like it has been done before by but got nada.
Code:
'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
_________________________
Mart
- Chuck Norris once sold ebay to ebay on ebay.
|
Top
|
|
|
|
#149877 - 2005-10-13 04:49 PM
Re: Getting mailbox size and number of items from exchange.
|
Radimus
Moderator
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
|
Top
|
|
|
|
#149878 - 2005-10-13 05:03 PM
Re: Getting mailbox size and number of items from exchange.
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
It's in the error message (which I culled whilst translating):
Quote:
Make sure CDO 1.21 is installed.
Doh!
|
Top
|
|
|
|
#149879 - 2005-10-13 05:06 PM
Re: Getting mailbox size and number of items from exchange.
|
Radimus
Moderator
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
I do this... Code:
$Outlook = CreateObject("Outlook.Application") $NS = $Outlook.GetNamespace("MAPI") $Fldr = $NS.GetDefaultFolder(6) $Unread = $Fldr.UnReadItemCount $Outlook = 0 if $unread $=sendmessage(@wksta,"You have $unread unread email messages") endif
|
Top
|
|
|
|
#149881 - 2006-03-08 11:28 AM
Re: Getting mailbox size and number of items from exchange.
|
Mart
KiX Supporter
Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
|
It's been a while but I decided to give this a go once more. This is a sort of literal translation with some VB stuff left in it cause I don't know (yet) how to convert it to kix. Above these lines there is a line starting with !!!!. In total there are three lines I have no clue of how to convert them. Like said this is a literal translation and there might be more lines that do not work. I gave it a go and it errored out on the $oSession.Logon , , False, True, , True, $strProfileInfo line.
Any help is much appreciated.
Code:
Dim $oSession, $oInfoStores, $oInfoStore, $StorageUsed, $NumMessages, $strProfileInfo, $sMsg, $server, $mailbox
$server = "mailserver" $mailbox = @USERID
If $server = "" ?"Please specify a server." Sleep 5 Exit Else If $mailbox = "" ?"Please specify a mailbox." Sleep 5 Exit EndIf EndIf
;Create Session object. $oSession = CreateObject("MAPI.Session") If @ERROR $sMsg = "Error creating MAPI.Session." $sMsg = $sMsg + "Make sure CDO 1.21 is installed. " $sMsg = $sMsg + @ERROR + " " + @SERROR ?$sMsg Exit @ERROR EndIf
$strProfileInfo = $server + " " + $mailbox
;Log on. ;!!!! This line is untranslated and therefor gives errors !!!! $oSession.Logon , , False, True, , True, $strProfileInfo If @ERROR $sMsg = "Error logging on: " $sMsg = $sMsg +@ERROR + " " + @SERROR ?$sMsg ?"Server: " $server ?"Mailbox: " $mailbox $oSession = 0 Exit @ERROR EndIf
;Grab the information stores. $oInfoStores = $oSession.InfoStores If @ERROR $sMsg = "Error retrieving InfoStores Collection: " $sMsg = $sMsg + @ERROR + " " + @SERROR ?$sMsg ?"Server: " + $server ?"Mailbox: " + $mailbox $oInfoStores = 0 $oSession = 0 Exit @ERROR EndIf
;Loop through information stores to find the user's mailbox. For Each $oInfoStore in $oInfoStores If InStr($oInfoStore.Name, "Mailbox - ") ;!!!! This line gives errors !!!! $StorageUsed = $oInfoStore.Fields(&HE080003) If @ERROR $sMsg = "Error retrieving PR_MESSAGE_SIZE: " $sMsg = $sMsg + @ERROR " " + @SERROR ?$sMsg ?"Server: " + $server ?"Mailbox: " $mailbox $oInfoStore = 0 $oInfoStores = 0 $oSession = 0 Exit @ERROR EndIf ;!!!! This line gives errors !!!! $NumMessages = $oInfoStore.Fields(&H36020003) If@ERROR $sMsg = "Error Retrieving PR_CONTENT_COUNT: " $sMsg = $sMsg +@ERROR + " " + @SERROR ?sMsg ?"Server: " $server ?"Mailbox: " $mailbox $oInfoStore = 0 $oInfoStores = 0 $oSession = 0 Exit @ERROR EndIf $sMsg = "Storage Used in " $oInfoStore.Name $sMsg = $sMsg + " (bytes): " + $StorageUsed ?$sMsg ?"Number of Messages: " + $NumMessages EndIf Next
;Log off. $rc = $oSession.LogOff
;Clean up memory. $oInfoStore = 0 $oInfoStores = 0 $oSession = 0
Edited by Mart (2006-03-08 11:29 AM)
_________________________
Mart
- Chuck Norris once sold ebay to ebay on ebay.
|
Top
|
|
|
|
#149884 - 2006-03-08 02:13 PM
Re: Getting mailbox size and number of items from exchange.
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Something like: Code:
$oSession.Logon(,,0,1,,1,$strProfileInfo)
|
Top
|
|
|
|
#149886 - 2006-03-08 03:59 PM
Re: Getting mailbox size and number of items from exchange.
|
Benny69
Moderator
Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
|
I know that the proper format for a Hex number is like this: &E080003 &36020003 with out the 'H'
|
Top
|
|
|
|
#149887 - 2006-03-08 04:25 PM
Re: Getting mailbox size and number of items from exchange.
|
Mart
KiX Supporter
Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
|
Thanx all for the help. Got the last lines working with benny's suggestion. Learned something today about VB, and COM scripting
This is what I ended up with. I'll be tweaking it a bit if needed in the following days. Tested this on Windows 2000 server SP4 with Exchange SP3 from a Windows XP SP2 client. Admin privileges should not be needed as long as the mailbox to use belongs to the account the script runs on.
Code:
;This script logs on to a server that is running Exchange Server. ;It displays the current number of bytes that are used in the user's mailbox and the number of messages. ;This requires that CDO 1.21 is installed on the computer. ; Dim $oSession, $oInfoStores, $oInfoStore, $StorageUsed, $NumMessages, $strProfileInfo, $sMsg, $server, $mailbox ; ;Set mail server mailbox to query. $server = "put your mail server here" $mailbox = "put the mailbox to query here" ; ;Check if the server and mailbox are not empty. If $server = "" ?"Please specify a server." Sleep 5 Exit Else If $mailbox = "" ?"Please specify a mailbox." Sleep 5 Exit EndIf EndIf ; ;Create Session object. $oSession = CreateObject("MAPI.Session") If @ERROR $sMsg = "Error creating MAPI.Session." $sMsg = $sMsg + "Make sure CDO 1.21 is installed. " $sMsg = $sMsg + @ERROR + " " + @SERROR ?$sMsg Exit @ERROR EndIf ; ;Set the login string. $vbLf = Chr(10) $strProfileInfo = $server + $vbLf + $mailbox ; ;Log on to the server. $oSession.Logon(,,0,1,,1,$strProfileInfo) If @ERROR $sMsg = "Error logging on: " $sMsg = $sMsg +@ERROR + " " + @SERROR ?$sMsg ?"Server: " $server ?"Mailbox: " $mailbox $oSession = 0 Exit @ERROR EndIf ; ;Grab the information stores. $oInfoStores = $oSession.InfoStores If @ERROR $sMsg = "Error retrieving InfoStores Collection: " $sMsg = $sMsg + @ERROR + " " + @SERROR ?$sMsg ?"Server: " + $server ?"Mailbox: " + $mailbox $oInfoStores = 0 $oSession = 0 Exit @ERROR EndIf ; ;Loop through information stores to find the user's mailbox. For Each $oInfoStore in $oInfoStores If InStr($oInfoStore.Name, "Mailbox - ") ; ;Get the amount of space ussed by the mailbox. $StorageUsed = $oInfoStore.Fields(&E080003) If @ERROR $sMsg = "Error retrieving PR_MESSAGE_SIZE: " $sMsg = $sMsg + @ERROR " " + @SERROR ?$sMsg ?"Server: " + $server ?"Mailbox: " $mailbox $oInfoStore = 0 $oInfoStores = 0 $oSession = 0 Exit @ERROR EndIf ; ;Get the number of messages in the mailbox. $NumMessages = $oInfoStore.Fields(&36020003) If@ERROR $sMsg = "Error Retrieving PR_CONTENT_COUNT: " $sMsg = $sMsg +@ERROR + " " + @SERROR ?"Server: " $server ?"Mailbox: " $mailbox $oInfoStore = 0 $oInfoStores = 0 $oSession = 0 Exit @ERROR EndIf ; ;Display the results. $sMsg = "Storage Used in " + $oInfoStore.Name $sMsg = $sMsg + " (bytes): " + $StorageUsed ?$sMsg ?"Number of Messages: " + $NumMessages Sleep 5 EndIf Next ; ;Log off. $rc = $oSession.LogOff ; ;Clean up memory. $oInfoStore = 0 $oInfoStores = 0 $oSession = 0
[edit] Removed a typo. [/edit]
Edited by Mart (2006-03-09 11:37 AM)
_________________________
Mart
- Chuck Norris once sold ebay to ebay on ebay.
|
Top
|
|
|
|
#149891 - 2006-06-20 08:37 PM
Re: Getting mailbox size and number of items from exchange.
|
dorion
Lurker
Registered: 2006-06-14
Posts: 1
|
This is a great little peice of code, my only problem is, $StorageUsed = "" + $oInfoStore.Fields(&E080003). $StorageUsed says its an object handle, I can make it a string, but as soon as I print it out it becomes an int. The problem is we want to catch people with 2+ gigabytes on their outlook account. We can catch 2-4 gigabytes, but 4-6 gigabytes is when the bytes wrap all the way around and become positive again. Does anyone know of someway I can find out if they definitely have more than 2+ gigabyes?
|
Top
|
|
|
|
#149893 - 2006-06-20 09:06 PM
Re: Getting mailbox size and number of items from exchange.
|
Mstudinski
Fresh Scripter
Registered: 2006-05-19
Posts: 25
|
I don't mean to offend anyone here but why not just use ESM for this. Less heartache than having to script and it's what it is designed for. I am more of an admin than a script writer so that may be why I think this way.
Matt
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 369 anonymous users online.
|
|
|