Hack 21 Send OU Information in Active Directory to an HTML Page

figs/moderate.gif figs/hack21.gif

Here's a terrific way to quickly display all the organizational units (OUs) in a domain.

If your Active Directory (AD) domains have a lot of OUs in them, it's easy to lose track of them, especially if you have OUs nested within OUs. This handy script generates an HTML page of all OUs in your current AD domain showing their path, description, and creation date. This information not only tells you which OUs you have in your domain, it also tells you which OUs contain other OUs, so you can easily create a map of the OU structure of your domain.

The Code

Just open Notepad or some other text editor (with Word Wrap disabled), type the following script, and save it with a .vbs extension as OU2HTML.vbs:

On Error Resume Next



Dim Root,Domain,wshNetwork

Dim oFileSys,fh



Set Root = GetObject("LDAP://RootDSE")

DomainPath = Root.Get("DefaultNamingContext")

Set Domain = GetObject("LDAP://" & DomainPath)

set wshNetwork=CreateObject("Wscript.Network")



myDomain=wshNetwork.UserDomain



htmlfile=myDomain & "-OUs.htm"



Set oFileSys=CreateObject("Scripting.FileSystemObject")

Set fh=oFileSys.CreateTextFile(htmlfile)



fh.WriteLine "<HTML><Title>" & myDomain & " Organizational Units</Title>"

fh.WriteLine "<Body><Font Size=+1>" & myDomain & " & _

"Organizational Units </Font><HR>"

fh.WriteLine "<Table Border=1 BorderColor=Blue CellSpacing=0><TR>"

fh.WriteLine "<TD BGColor=Blue><Font Color=White><P Align=Center> " & _

"<B>OU</B></TD>"

fh.WriteLine "<TD BGColor=Blue><Font Color=White><P Align=Center><B>Description</B></TD>"

fh.WriteLine "<TD BGColor=Blue><Font Color=White><P Align=Center> " & _

"<B>Path</B></TD>"

fh.WriteLine "<TD BGColor=Blue><Font Color=White><P Align=Center> " & _

"<B>Created</B></TD></TR>"



wscript.echo "Getting OU information for " & mydomain & "..." & _

EnumOU Domain.ADSPath



fh.WriteLine "</Table><Font Size=-1><I>Page Generated " & Now & " _

"</I></Font>"

fh.WriteLine "</Body></HTML>"

fh.close



wscript.echo "Output has been sent to " & htmlfile



Set oFileSys=Nothing

Set fh=Nothing

Set domain=Nothing

Set Root=Nothing

Set wshNetwork=Nothing



wscript.quit



'*****************************************

Sub EnumOU(objPath)



'On Error Resume Next



Set objPath = GetObject(objPath)



objPath.Filter=Array("organizationalUnit")



For Each item in objPath

If item.Description="" Then

ouDescription="N/A"

Else

ouDescription=item.Description

End If



fh.writeLine "<TR><TD>" & MID(item.Name,4) & "</TD><TD>" & ouDescription & _

"</TD><TD>" & item.ADSPath & "</TD><TD>" & GetCreated(item.ADSPath) & "</TR>"

'Uncomment next line for debugging purposes

' wscript.echo item.Name & vbTab & item.Description & vbTab & item.ADSPath



'Iterate through

EnumOU item.ADSPath



Next



Set objPath=Nothing



End Sub



'****************************

Function GetCreated(objPath)

On Error Resume Next



Set objDetail=GetObject(objPath)

Set objSchema=GetObject(objDetail.Schema)



For Each z in objSchema.OptionalProperties

Set adsProperty = GetObject("LDAP://Schema/" & z)

If z="whenCreated" Then

strCreated = objDetail.Get(z)

GetCreated=strCreated

'wscript.echo "Created " & strCreated

strValue=""

End If

Next



End Function

Running the Hack

To run the script, simply create a shortcut to the script, double-click on the shortcut, and follow the prompts provided by the dialog boxes the script generates. When the script runs, it creates an HTML page in the same directory in which the script itself is located. The name of this HTML page is domain-OUs.htm, where domain is the name of your domain. Figure 2-6 shows a sample HTML page created for a test domain named mtit.com.

Figure 2-6. OUs in the mtit.com domain
figs/wsh_0206.gif

It's easy to see from the Path column in Figure 2-6 that the Local and National OUs are contained within the Sales OU.

?Hans Schefske