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

Testing and Sending email via SMTP using Opportunistic TLS and oAuth in Office365 with PowerShell

As well as EWS and Remote PowerShell (RPS) other mail protocols POP3, IMAP and SMTP have had OAuth authentication enabled in Exchange Online (Official announcement here ). A while ago I created  this script that used Opportunistic TLS to perform a Telnet style test against a SMTP server using SMTP AUTH. Now that oAuth authentication has been enabled in office365 I've updated this script to be able to use oAuth instead of SMTP Auth to test against Office365. I've also included a function to actually send a Message. Token Acquisition  To Send a Mail using oAuth you first need to get an Access token from Azure AD there are plenty of ways of doing this in PowerShell. You could use a library like MSAL or ADAL (just google your favoured method) or use a library less approach which I've included with this script . Whatever way you do this you need to make sure that your application registration  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

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 I wrote th

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 Graph is limited to a m
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.