Skip to main content

Reporting on the number of Users, Contacts and Groups in a OU and sub OU’s

Somebody asked how to do this last week and I’ve had a few other question’s lately on reporting based on Sub Ou’s so I thought I’d put together a sample to show one method of doing this. The method I’ve used is to create a data-shape of all the objects with a Ou and its Sub Ou’s and then relate this information to an enumeration of the OU tree.

What this script does is takes the name of the OU where you want to start the query at as a command-line parameter. It then does an ADSI query to find the distinguished name of the first OU that matches the text entered. It then does 3 separate subtree queries of all the Users, contacts and group objects that are located under the root OU. To group the information that is returned this is where the ADO datashaping provider is used. The information is then stored temporary in a multi dimensional array and then used to finally build a HTM report called “c:\temp\report.htm” which has a table that shows the OU Name, Description, Path and the number of users, groups and contacts within each OU.

The script takes one command-line parameter which is the name of the OU you want to run it against eg to run the script “cscript.exe lobjectsv2.vbs OUname”. I’ve also created a provision to let the script be run against the entire Active Directory domain instead of just and OU branch to do this instead of using the name of the OU as a command-line parameter use rootdse as the command-line parameter eg “cscript lobjectsv2.vbs rootdse”

I’ve put a downloadable copy of the code here the script looks like


queryou = wscript.arguments(0)
report = "<table border=""1"" width=""100%"">" & vbcrlf
report = report & " <tr>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">OU
Name</font></b></td>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">OU
Description</font></b></td>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">OU
Path</font></b></td>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">#
Users</font></b></td>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">#
Contacts</font></b></td>" & vbcrlf
report = report & "<td align=""center"" bgcolor=""#000080""><b><font color=""#FFFFFF"">#
Groups</font></b></td>" & vbcrlf
report = report & "</tr>" & vbcrlf
set conn = createobject("ADODB.Connection")
set com = createobject("ADODB.Command")
Set iAdRootDSE = GetObject("LDAP://RootDSE")
strNameingContext = iAdRootDSE.Get("defaultNamingContext")
Conn.Provider = "ADsDSOObject"
Conn.Open "ADs Provider"
ouQuery = "<LDAP://" & ou & strNameingContext & ">;(&(objectCategory=organizationalUnit)(ou="
& queryou & "));description,name,distinguishedName;subtree"
Com.ActiveConnection = Conn
Com.CommandText = ouQuery
Set ouRs = Com.Execute
if ouRs.recordcount = 0 and queryou <> "rootdse" Then
wscript.echo "Cant find Ou"
Else
If queryou = "rootdse" Then
ou = strNameingContext
Else
ou = ouRs.fields("distinguishedName")
End if
Wscript.echo "Users under " & ou
wscript.echo
uarray = ReportonOU("user")
Wscript.echo
Wscript.echo "Contacts under " & ou
wscript.echo
carray = ReportonOU("contact")
garray = ReportonOU("group")

For uic = LBound(uarray,3) To UBound(uarray,4)
report = report & "<tr>" & vbcrlf
report = report & "<td align=""center"">" & uarray(1,1,1,uic) & "&nbsp;</td>" &
vbcrlf
report = report & "<td align=""center"">" & uarray(1,1,0,uic) & "&nbsp;</td>" &
vbcrlf
report = report & "<td align=""center"">" & uarray(1,0,0,uic) & "&nbsp;</td>" &
vbcrlf
report = report & "<td align=""center"">" & uarray(0,0,0,uic) & "&nbsp;</td>" &
vbcrlf
report = report & "<td align=""center"">" & carray(0,0,0,uic) & "&nbsp;</td>" &
vbcrlf
report = report & "<td align=""center"">" & garray(0,0,0,uic) & "&nbsp;</td>" &
vbcrlf
report = report & "</tr>" & vbcrlf
Next
report = report & "</table>" & vbcrlf
Set fso = CreateObject("Scripting.FileSystemObject")
set wfile = fso.opentextfile("c:\temp\report.htm",2,true)
wfile.write report
wfile.close
set wfile = nothing
set fso = nothing
End If


function ReportonOU(objecttype)

set conn1 = createobject("ADODB.Connection")
strConnString = "Data Provider=NONE; Provider=MSDataShape"
conn1.Open strConnString
set objParentRS = createobject("adodb.recordset")
set objChildRS = createobject("adodb.recordset")
strSQL = "SHAPE APPEND" & _
" NEW adVarChar(255) AS SOADDisplayName, " & _
" NEW adVarChar(255) AS SOADDescription, " & _
" NEW adVarChar(255) AS SOADDN, " & _
" ((SHAPE APPEND " & _
" NEW adVarChar(255) AS MOADDisplayName, " & _
" NEW adVarChar(255) AS MOADDN " & ")" & _
" RELATE SOADDN TO MOADDN) AS rsSOMO "
objParentRS.LockType = 3
objParentRS.Open strSQL, conn1

adsiQuery = "<LDAP://" & ou &
">;(objectCategory=organizationalUnit);description,name,distinguishedName;subtree"
Com.ActiveConnection = Conn
Com.CommandText = adsiQuery
Set Rs = Com.Execute
While Not Rs.EOF
objParentRS.addnew
objParentRS("SOADDisplayName") = rs.fields("name")
If Not IsNull(rs.fields("description").value) then
For Each val In rs.fields("description").value
objParentRS("SOADDescription") = val
Next
Else
objParentRS("SOADDescription") = " "
End if
objParentRS("SOADDN") = LCase(rs.fields("distinguishedName"))
objParentRS.update
rs.movenext
Wend

If objecttype = "group" then
objQuery = "<LDAP://" & ou &
">;(&(mailnickname=*)(|(objectCategory=group)));description,name,cn,distinguishedName;subtree"
Else
objQuery = "<LDAP://" & ou &
">;(&(mailnickname=*)(|(&(objectCategory=person)(objectClass=" & objecttype &
"))));description,name,cn,distinguishedName;subtree"
End if
Com.ActiveConnection = Conn
Com.CommandText = objQuery
Set Rs1 = Com.Execute
Set objChildRS = objParentRS("rsSOMO").Value
While Not Rs1.EOF
objChildRS.addnew
objChildRS("MOADDisplayName") = rs1.fields("name")
objChildRS("MOADDN") = Replace(LCase(rs1.fields("distinguishedName")),"cn=" &
LCase(rs1.fields("cn")) & ",","")
objChildRS.update
rs1.movenext
Wend
anum = cInt(objParentRS.recordcount)
reDim objcntarray(1,1,1,anum)
i = 0
objParentRS.MoveFirst
Do While Not objParentRS.EOF
Set objChildRS = objParentRS("rsSOMO").Value
wscript.echo objParentRS("SOADDisplayName") & " - " &
objParentRS("SOADDescription") & " : "& objChildRS.recordCount
objcntarray(1,1,1,i) = objParentRS("SOADDisplayName")
objcntarray(1,1,0,i) = objParentRS("SOADDescription")
objcntarray(1,0,0,i) = objParentRS("SOADDN")
objcntarray(0,0,0,i) = objChildRS.recordCount
objParentRS.movenext
i = i + 1
Loop
ReportonOU = objcntarray
objParentRS.close
objChildRS.close
rs.close
rs1.close
Set objParentRS = Nothing
Set objChildRS = Nothing
set conn1 = Nothing
Set Rs1 = Nothing
Set rs = Nothing

End function

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Gr...

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You ...

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why ...
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.