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

Downloading a shared file from Onedrive for business using Powershell

I thought I'd quickly share this script I came up with to download a file that was shared using One Drive for Business (which is SharePoint under the covers) with Powershell. The following script takes a OneDrive for business URL which would look like https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filename.txt This script is pretty simple it uses the SharePoint CSOM (Client side object Model) which it loads in the first line. It uses the URI object to separate the host and relative URL which the CSOM requires and also the SharePointOnlineCredentials object to handle the Office365 SharePoint online authentication. The following script is a function that take the OneDrive URL, Credentials for Office365 and path you want to download the file to and downloads the file. eg to run the script you would use something like ./spdownload.ps1 ' https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filena

Export calendar Items to a CSV file using EWS and Powershell

Somebody asked about this last week and while I have a lot of EWS scripts that do access the Calendar I didn't have a simple example that just exported a list of the Calendar events with relevant information to a CSV file so here it is. I've talked on this one before in this howto  but when you query the calendar folder using EWS you need to use a CalendarView which will expand any recurring appointments in a calendar. There are some limits when you use a calendarview in that you can only return a maximum of 2 years of appointments at a time and paging will limit the max number of items to 1000 per call. So if you have a calendar with a very large number of appointments you need to break your query into small date time blocks. In this example script I'm just grabbing the next 7 days of appointments if you want to query a longer period you need to adjust the following lines (keeping in mind what I just mentioned) #Define Date to Query $StartDate = (Get-Date) $EndDate

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
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.