O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Windows Server Hacks
By Mitch Tulloch
March 2004
More Info

HACK
#21
Send OU Information in Active Directory to an HTML Page
Here's a terrific way to quickly display all the organizational units (OUs) in a domain
The Code
[Discuss (2) | Link to this hack]

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


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.