Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Enumerate Parent OU and Sub OU

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,23974
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Enumerate Parent OU and Sub OU
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Enumerate Parent OU and Sub OU - 7/24/2005 2:37:20 AM   
  ThePariah

 

Posts: 15
Score: 0
Joined: 6/14/2005
From: United Kingdom
Status: offline
Hi,

I would like to enumerate a Parent OU and Sub OU's. What i would like to do then is use the data collected during the enumeration to populate a dropdown box in and hta file. The enumeration process using ADO is straight foward enough:

METHOD ONE.



' Use ADO to search Active Directory.

Set
objCommand = CreateObject("ADODB.Command")

Set
objConnection = CreateObject("ADODB.Connection")
objConnection.Provider =
"ADsDSOObject"
objConnection.Open
"Active Directory Provider"
objCommand.ActiveConnection = objConnection
strBase =
"<LDAP://ou=Managed Objects,dc=ignite,dc=co,dc=uk>"



' Filter on Organizational Units.

strFilter =
"(objectCategory=organizationalUnit)"
strAttributes =
"distinguishedName"
strQuery = strBase &
";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties(
"Page Size") = 112113
objCommand.Properties(
"Timeout") = 126127
objCommand.Properties(
"Cache Results") = False

Set
objRecordSet = objCommand.Execute
 

' Enumerate the recordset.

Do
Until objRecordSet.EOF
strDN = objRecordSet.Fields(
"distinguishedName")
Wscript.Echo strDN
objRecordSet.MoveNext

Loop

 

' Clean up.

objConnection.Close


Here is another Method that was suggested to me:

METHOD TWO.


Set
colItems = GetObject _
(
"LDAP://ou=[Parent OU],dc=[Domain Name],dc=co,dc=uk"
)
colItems.
Filter = Array("organizationalUnit"
)

For
Each objItem in colItems
Wscript.Echo objItem.cn

Next


What i am unsure about is how to gather that information and use it to populate my dropdown. I had thought about creating a temporary text file writing the results to a text file and then reading the text file to populate my dropdown but i feel this is over complicating the situation.

Does anyone have any other suggestions? Can anyone help?

Thanks.

 
 
Post #: 1
 
 RE: Enumerate Parent OU and Sub OU - 7/24/2005 6:54:37 PM   
  ehvbs

 

Posts: 2114
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
If METHOD TWO works for you, try:

  Set colItems = GetObject( "LDAP://ou=[Parent OU],dc=[Domain Name],dc=co,dc=uk" )
  colItems.Filter = Array("organizationalUnit")
 
  Dim oCBX, oOPT
 
  Set oCBX = document.all( "NameOrIdOfCBX" )

  For Each objItem in colItems
      Wscript.Echo objItem.cn
      Set oOPT   = oCBX.document.createElement( "OPTION" )
      oOPT.Text  = objItem.cn
      oOPT.Value = objItem.cn
      oCBX.Options.Add oOPT
     
  Next

[not testet]

(in reply to ThePariah)
 
 
Post #: 2
 
 RE: Enumerate Parent OU and Sub OU - 8/1/2005 10:21:27 AM   
  ThePariah

 

Posts: 15
Score: 0
Joined: 6/14/2005
From: United Kingdom
Status: offline
Following Lengthy research and posts to newsgroups i have managed to work out how to achieve what i wanted. I post the solution here for the benefit of everyone else.

<HTML>
<HEAD>
<TITLE>Populate Dropdown</TITLE>
<HTA:APPLICATION
APPLICATION ID="PopulateDropdown"
APPLICATIONNAME="PopulateDropdown"
BORDER = "thin"
CAPTION = "yes"
RESIZE = "no"
ICON = "sprocket.ico"
SHOWINTASKBAR = "yes"
SINGLEINSTANCE = "yes"
SYSMENU = "yes"
WINDOWSTATE = "normal"
SCROLL = "yes"
SCROLLFLAT = "yes"
VERSION = "1.0"
INNERBORDER = "no"
SELECTION = "no"
MAXIMIZEBUTTON = "no"
MINIMIZEBUTTON = "yes"
NAVIGABLE = "yes"
CONTEXTMENU = "yes"
BORDERSTYLE = "normal">
</HTA>

<style>
select
{
  font-family: verdana;
  font-size: 8pt;
  width: 300px;
  margin-left: 0px;
}
.button
{
font-family: verdana;
font-size: 8pt;
}
</style>
<SCRIPT LANGUAGE="VBScript">
Sub Window_Onload

check_button.disabled=True
OUSelector.InnerHTML = SelectTargetOU()

end Sub

Sub EnableCheck()
check_button.disabled=False
End Sub
'----------------------------------------------------------'
' Builds a <SELECT> form with all OUs in canonical format. '
'----------------------------------------------------------'
Function SelectTargetOU
Const adVarChar = 200
Dim defaultNC, AdConn, AdComm, AdRs, AdRoot, AdQuery
Dim SortRs
SelectTargetOU = "<SELECT onChange='EnableCheck()' NAME='TargetOU' SIZE='1'><OPTION VALUE=''/>-- Select an Organizational Unit --"
defaultNC = GetObject("LDAP://RootDSE").Get("DefaultNamingContext")
AdQuery = "SELECT AdsPath, canonicalName " & _
       " FROM 'LDAP://" & defaultNC & "'" & _
       " WHERE objectCategory='organizationalUnit'"

'-----------------------------------------------------------------------'
' Create a disconnected recordset to sort the results because AD can't. '
'-----------------------------------------------------------------------'
Set SortRs = CreateObject("ADOR.Recordset")
SortRs.fields.append "AdsPath",adVarChar,255
SortRs.fields.append "canonicalName",adVarChar,255
SortRs.open
'-------------------------------------------------------------------'
' Create ADOBO connection and command objects and set search options'
'-------------------------------------------------------------------'
Set AdConn = CreateObject("ADODB.Connection")
AdConn.Provider = "ADsDSOObject"
AdConn.Open "Active Directory Provider"
Set AdComm = CreateObject("ADODB.Command")
AdComm.ActiveConnection = AdConn
AdComm.Properties("SearchScope") = 2
AdComm.Properties("Page Size") = 500
'---------------------------------------'
' Set query text and execute the query. '
'---------------------------------------'
AdComm.CommandText = AdQuery
Set AdRs = AdComm.Execute
With AdRs
While Not .EOF
AdsPath = .Fields("AdsPath")
canonicalName = .Fields("canonicalName")
SortRs.AddNew
SortRs.Fields("AdsPath") = AdsPath
SortRs.Fields("canonicalName") = canonicalName(0)
SortRs.Update
.MoveNext
Wend
End With

'------------------------------------------------------------------------------------------'
' Now sort the disconnected record set so it will make sense and build <OPTION> selements. '
'------------------------------------------------------------------------------------------'
With SortRs
.Sort = "canonicalName asc"
.MoveFirst
While Not .EOF
AdsPath = .Fields("AdsPath")
canonicalName = .Fields("canonicalName")
SelectTargetOU = SelectTargetOU & "<OPTION VALUE='" & AdsPath & "'/>" & canonicalName & vbCrLf
.MoveNext
Wend
End With
SelectTargetOU = SelectTargetOU & "</SELECT>"
Set AdConn = Nothing
Set AdComm = Nothing
Set AdRs = Nothing
End Function
Sub CheckOU
msgbox "You Selected: " & TargetOU.Value
End Sub
</SCRIPT>
</HEAD>
<BODY bgcolor="#E5ECF9">
<table>
  <TD>
     <SPAN ID='OUSelector'>&nbsp;</SPAN>
     <input id=checkbutton class="button" type="button" value="Check"  name="check_button"  onClick="CheckOU">
     <input id=quitbutton class="button" type="button" value="Quit" name="quit_button" onClick="Self.Close">
  </td>
</table>
</BODY>
</HTML>

(in reply to ThePariah)
 
 
Post #: 3
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Enumerate Parent OU and Sub OU Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts