Page 1 of 2 12>
Topic Options
#149875 - 2005-10-13 02:56 PM Getting mailbox size and number of items from exchange.
Mart Moderator Offline
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
#149876 - 2005-10-13 04:40 PM Re: Getting mailbox size and number of items from exchange.
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Can't get past "Set oSession = CreateObject("MAPI.Session")" - fails in both KiXtart and VB for me.

Must be missing a provider or activex control or something.

Top
#149877 - 2005-10-13 04:49 PM Re: Getting mailbox size and number of items from exchange.
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
it works in vbs for me
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#149878 - 2005-10-13 05:03 PM Re: Getting mailbox size and number of items from exchange.
Richard H. Administrator Offline
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 Offline
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

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#149880 - 2005-10-13 11:12 PM Re: Getting mailbox size and number of items from exchange.
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Guys,

Good to know someone is trying to put it into kix. Like said I tried and failed because of my low level of vb knowledge.

If you get anywhere please post.
Would like to implement it into a script similar to Doc’s Computer Inventory script.
Don't know about the other two examples on the MS page. Maybe they are easier to convert?
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#149881 - 2006-03-08 11:28 AM Re: Getting mailbox size and number of items from exchange.
Mart Moderator Offline
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
#149882 - 2006-03-08 12:25 PM Re: Getting mailbox size and number of items from exchange.
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 309
Loc: STRASBOURG, France
try to modify the line before "oSession.logon" :
$strProfileInfo = $server + " " + $mailbox

this is not correct because in vb, this is
strProfileInfo = server & vblf & mailbox

you should try :
$vbLf = Chr(10)
$strProfileInfo = $server + $vbLf + $mailbox

look at Logon Method (Session Object)
for more informations.
_________________________
Christophe

Top
#149883 - 2006-03-08 01:28 PM Re: Getting mailbox size and number of items from exchange.
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Thanx for the link. I understand the login part much better now. Fixed the part you suggested.
But it still errors out on the same line cause it is not in kixtart format (unexpected command).

As you might have noticed I have very limited knowledge on this part so I have got no idea what it should be in order to make it work.
_________________________
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 Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Something like:
Code:
$oSession.Logon(,,0,1,,1,$strProfileInfo)


Top
#149885 - 2006-03-08 03:22 PM Re: Getting mailbox size and number of items from exchange.
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Sweet. Sounds logical and seems to work.

Now it stops at the other two lines. These lines actually get the data from a field in the exchange DB as far as I can see. Tried several ways of formatting the lines but none worked.

Now these lines look like this $StorageUsed = $oInfoStore.Fields(&HE080003) and $NumMessages = $oInfoStore.Fields(&H36020003) both are not proper kix syntax so they give an error. Sorry to keep on asking but anyone have an idea how to convert this to kix format?


Edited by Mart (2006-03-08 03:23 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#149886 - 2006-03-08 03:59 PM Re: Getting mailbox size and number of items from exchange.
Benny69 Offline
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'
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#149887 - 2006-03-08 04:25 PM Re: Getting mailbox size and number of items from exchange.
Mart Moderator Offline
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
#149888 - 2006-03-08 08:10 PM Re: Getting mailbox size and number of items from exchange.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Exchange SP3 (So that would be Exchange 2000? )

Suppose it doesn't work with 5.5 - but I've learned how to export all that information anyways so not too big an issue.

This type of script might be cool though for on the fly checking as an Admin.

Thanks for sharing.

Top
#149889 - 2006-03-08 08:21 PM Re: Getting mailbox size and number of items from exchange.
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Yep that would be Exchange 2000. MS says only Exchange 2000 and up supports this like this.
I'm going to integrate it into a script like your computer info script. Users click on a shortcut on the desktop and get all the info needed when they call us. It's always a pain when you ask a user for the name of the system he/she is on, what printers they have if they can't print and don’t say where they want to print, etc...... Now when they call all info will be on the screen so if they can read then they can tell us what is on the screen.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#149890 - 2006-03-08 08:53 PM Re: Getting mailbox size and number of items from exchange.
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11624
Loc: CA
Sounds good, yeah while on that subject I hope to modify my script again soon to support CSV/HTML/On-Screen/e-mail if I can figure out a good method to support all those selections.
 
Not so sure why one would want a CSV though as that is a LOT of data that one could only maybe import back into an database or excel, but even in Excel or DB it would be a pain to format it all for easy reading.

Good for DB though if one has only specific data they are searching for.

Top
#149891 - 2006-06-20 08:37 PM Re: Getting mailbox size and number of items from exchange.
dorion Offline
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
#149892 - 2006-06-20 08:48 PM Re: Getting mailbox size and number of items from exchange.
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
string doesn't become a int.
it is likely that dbl becomes an int when passed to a string though.
shouldn't, but it's possible.
_________________________
!

download KiXnet

Top
#149893 - 2006-06-20 09:06 PM Re: Getting mailbox size and number of items from exchange.
Mstudinski Offline
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
#149894 - 2006-06-20 09:31 PM Re: Getting mailbox size and number of items from exchange.
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
am I stupid or...

can you export a report of mailbox sizes from it by calling it via commandline?
_________________________
!

download KiXnet

Top
Page 1 of 2 12>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 369 anonymous users online.
Newest Members
rrosell, PatrickPinto, Raoul, Timothy, Jojo67
17877 Registered Users

Generated in 0.074 seconds in which 0.025 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org