Tuesday, March 15, 2005

Data shaping ADSI and Exchange WMI information

I blogged last week about relating mailbox size data with mailbox quota data here. The main problem I had with the approach that I was using here is although it worked fine for 1 user when you wanted to relate a lot of data say the size of everybody’s mailbox on one particular server to the possible size of the mailbox quotas from either their account or mailbox store it gets a little slow.

Data shaping is an ADO thing that allows you to build hierarchical relationships between two or more logical entities in a query. What this means is that I can take two or more separate data sources (via a disconnected recordset) and bung them together in a hierarchical tree (as long as you have a field that relates between the two sources).

The guys over at Rolla do a lot better job of explaining it them me have a look at http://www.4guysfromrolla.com/webtech/060301-1.shtml and

In my particular scenario I want to shape data from 3 different data sources
1. All the mail store objects for a particular server from the Configuration partition in AD – via ADSI
2. All the visible mailboxes (AD- User object) on a server – Via ADIS
3. All the mailbox sizes – Via Exchange_Mailbox WMI on Exchange 2003

Essentially the hierarchy I wanted to create would look like this

Mail Store Quotas -+
------------------------+---User Account Quotas -+
---------------------------------------------------------+--- Mailbox Size

Because the providers I’m using aren’t going to support the Shape command directly using offline recordsets provides the best method of putting the data into a format that can be shaped.

So what the script does is takes the server-name as a command line parameter and first does a query of Active Directory to get the full distinguished Name of the server. This is then used to find all the mail-stores that belong to this particular server. The information about the mail-store quotas of each of the mail-stores on this server becomes the parent record-set. The next ADSI query that is run is a query of the visible mailboxes in the GAL that are on this server. This information then becomes the child record-set and relates to the parent record-set based on the HomeMDB property of the user object and the DN or the mailstore. The last query to be performed is a grab of all the mailbox sizes on the server from the Exchange_Mailbox WMI class. This information becomes the grandchild record-set and relates to the child recod-set though the legancyDN property of the WMI instance and the AD legacyExchangeDN property of the user object.

The output of the code is all piped to a csv file that by default will be saved to the root of the c:\ drive. Some quick math is done of the mailbox size and quotas size to work out the percentage of the quota that is being used.

In its script form I’m not sure how useful this code is, I hope to turn it into a little Asp.net app that a helpdesk person could use to manage the quota’s for all user's on a server. The other approach maybe to filter the records that are returned further so say you only look at people who are currently at 90 % of there quota and then send a mail to your helpdesk operators to chase up etc.

I’ve posted a download copy of the code here

The script looks like

servername = wscript.arguments(0)
set conn = createobject("ADODB.Connection")
set com = createobject("ADODB.Command")
set conn1 = createobject("ADODB.Connection")
strConnString = "Data Provider=NONE; Provider=MSDataShape"
conn1.Open strConnString
Set iAdRootDSE = GetObject("LDAP://RootDSE")
strNameingContext = iAdRootDSE.Get("configurationNamingContext")
strDefaultNamingContext = iAdRootDSE.Get("defaultNamingContext")
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.opentextfile("c:\exportquotas.csv",2,true)
Size(MB),Percent Used")
set objParentRS = createobject("adodb.recordset")
set objChildRS = createobject("adodb.recordset")
" NEW adVarChar(255) AS SOADDisplayName, " & _
" NEW adVarChar(255) AS SOADDistName, " & _
" NEW adVarChar(255) AS SOmDBStorageQuota, " & _
" NEW adVarChar(255) AS SOmDBOverQuotaLimit, " & _
" NEW adVarChar(255) AS SOmDBOverHardQuotaLimit, " & _
" ((SHAPE APPEND " & _
" NEW adVarChar(255) AS MOADDisplayName, " & _
" NEW adVarChar(255) AS MOADLegacyDN, " & _
" NEW adVarChar(255) AS MOADHomeMDB, " & _
" NEW adBoolean AS MOmDBUseDefaults, " & _
" NEW adVarChar(255) AS MOmDBStorageQuota, " & _
" NEW adVarChar(255) AS MOmDBOverQuotaLimit, " & _
" NEW adVarChar(255) AS MOmDBOverHardQuotaLimit, " & _
" ((SHAPE APPEND " & _
" NEW adVarChar(255) AS WMILegacyDN, " & _
" NEW adVarChar(255) AS WMISize, " & _
" NEW adVarChar(255) AS WMIStorename) " & _
")" & _
objParentRS.LockType = 3
objParentRS.Open strSQL, conn1
Conn.Provider = "ADsDSOObject"
Conn.Open "ADs Provider"
svcQuery = "<LDAP://" & strNameingContext & ">;(&(objectCategory=msExchExchangeServer)(cn="
& Servername & "));cn,name,distinguishedName,legacyExchangeDN;subtree"
Com.ActiveConnection = Conn
Com.CommandText = svcQuery
Set Rs = Com.Execute
while not rs.eof
sgQuery = "<LDAP://" & strNameingContext & ">;(&(objectCategory=msExchPrivateMDB)(msExchOwningServer="
& rs.fields("distinguishedName") & "));cn,name,displayname,distinguishedName,
Com.CommandText = sgQuery
Set Rs1 = Com.Execute
while not rs1.eof
objParentRS("SOADDisplayName") = rs1.fields("displayname")
objParentRS("SOADDistName") = left(rs1.fields("distinguishedName"),255)
objParentRS("SOmDBStorageQuota") = rs1.fields("mDBStorageQuota")
objParentRS("SOmDBOverQuotaLimit") = rs1.fields("mDBOverQuotaLimit")
objParentRS("SOmDBOverHardQuotaLimit") = rs1.fields("mDBOverHardQuotaLimit")
wscript.echo "finished 1st AD query storage groups"
GALQueryFilter = "(&(&(&(& (mailnickname=*)(!msExchHideFromAddressLists=TRUE)(|
(&(objectCategory=person)(objectClass=user)(msExchHomeServerName=" &
rs.fields("legacyExchangeDN") & ")) )))))"
strQuery = "<LDAP://" & strDefaultNamingContext & ">;" & GALQueryFilter & ";distinguishedName,msExchMailboxGuid,displayname,
com.Properties("Page Size") = 100
Com.CommandText = strQuery
Set Rs2 = Com.Execute
objChildRS.LockType = 3
Set objChildRS = objParentRS("rsSOMO").Value
while not rs2.eof
objChildRS("MOADDisplayName") = rs2.fields("displayname")
objChildRS("MOADLegacyDN") = rs2.fields("legacyExchangeDN")
objChildRS("MOADHomeMDB") = left(rs2.fields("homemdb"),255)
objChildRS("MOmDBUseDefaults") = rs2.fields("mDBUseDefaults")
objChildRS("MOmDBStorageQuota") = rs2.fields("mDBStorageQuota")
objChildRS("MOmDBOverQuotaLimit") = rs2.fields("mDBOverQuotaLimit")
objChildRS("MOmDBOverHardQuotaLimit") = rs2.fields("mDBOverHardQuotaLimit")
wscript.echo "finished 2nd AD query mailbox"
Set objgrandchild = objChildRS("MOWMI").Value
strWinMgmts ="winmgmts:{impersonationLevel=impersonate}!//"& servername
Set objWMIExchange = GetObject(strWinMgmts)
Set listExchange_MailboxSizes = objWMIExchange.ExecQuery("Select * FROM
For each objExchange_MailboxSize in listExchange_MailboxSizes
objgrandchild("WMILegacyDN") = objExchange_MailboxSize.LegacyDN
objgrandchild("WMISize") = objExchange_MailboxSize.size
objgrandchild("WMIStorename") = objExchange_MailboxSize.storename
wscript.echo "finished 3nd Exchange WMI query"
Do While Not objParentRS.EOF
Set objChildRS = objParentRS("rsSOMO").Value
strQuota = objParentRS("SOmDBStorageQuota")
if isnull(objParentRS("SOmDBStorageQuota")) then strQuota = 0
strQuotaLimit = objParentRS("SOmDBOverQuotaLimit")
if isnull(objParentRS("SOmDBOverQuotaLimit")) then strQuotaLimit = 0
strHardQuotaLimit = objParentRS("SOmDBOverHardQuotaLimit")
if isnull(objParentRS("SOmDBOverHardQuotaLimit")) then strHardQuotaLimit = 0
Do While Not objChildRS.EOF
strUsername = replace(objChildRS("MOADDisplayName"),",","")
strUsedefaults = objChildRS("MOmDBUseDefaults")
strusrQuota = objChildRS("MOmDBStorageQuota")
if isnull(objChildRS("MOmDBStorageQuota")) then strusrQuota = 0
strusrQuotaLimit = objChildRS("MOmDBOverQuotaLimit")
if isnull(objChildRS("MOmDBOverQuotaLimit")) then strusrQuotaLimit = 0
strusrHardQuotaLimit = objChildRS("MOmDBOverHardQuotaLimit")
if isnull(objChildRS("MOmDBOverHardQuotaLimit")) then strusrHardQuotaLimit = 0
Set objgrandchild = objChildRS("MOWMI").Value
Do While Not objgrandchild.EOF
mbsize = objgrandchild("WMISize")
if strUsedefaults = false then
if strusrQuota <> 0 then
wfile.writeline strUsername & "," &
replace(formatnumber(strusrQuota/1024,2),",","") & "," &
replace(formatnumber(strusrQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(strusrHardQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(mbsize/1024,2),",","") & "," &
formatnumber((mbsize/strusrQuota)*100,2) & "%"
wfile.writeline strUsername & "," &
replace(formatnumber(strusrQuota/1024,2),",","") & "," &
replace(formatnumber(strusrQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(strusrHardQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(mbsize/1024,2),",","") & "," & 0 & "%"
end if
if strQuota <> 0 then
wfile.writeline strUsername & "," &
replace(formatnumber(strQuota/1024,2),",","") & "," &
replace(formatnumber(strQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(strHardQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(mbsize/1024,2),",","") & "," &
formatnumber((mbsize/strQuota)*100,2) & "%"
wfile.writeline strUsername & "," &
replace(formatnumber(strQuota/1024,2),",","") & "," &
replace(formatnumber(strQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(strHardQuotaLimit/1024,2),",","") & "," &
replace(formatnumber(mbsize/1024,2),",","") & "," & 0 & "%"
end if
end if
Wscript.echo "CSV file created"


Joel Stidley said...


Thank you for posting your quotembsize script. I was looking around to write my own and you had already done it!

Keep up the good work!

Anonymous said...

What if the query returns no results? The .MoveFirst statement will generate an error. How do you check for that?

Glen said...

If the are no records in the parent recordset then this scripts pretty pointless it wont display anything. What you could do is check the recordcount of the recordset if you just want to get rid of the error message. Or doing thing like

while not objParentRS.eof


Anonymous said...

Great script this has really helped me out. One question I would like to ask is how could you add the AD company field to the output of this script?

Glen said...

Have a look at http://msgdev.mvps.org/exdevblog/adsicomp.zip this should do the trick


Anonymous said...


Is that possible to have a plug-in in the users outlook to show there mailbox size/quota-i.e a visual indicator.


Glen said...

OWA 2007 now display this information its a customized function for OWA only i havent't see it done in Outlook. You could build something yourself to do this though.


Anonymous said...

Thanks Glen,

Where i can find a c# version of this application?

Thanks again

Glen said...

You'll need to write your own (its really not that hard)



Anonymous said...

This is exactly what I was looking for thank you thank you!