Link to home
Start Free TrialLog in
Avatar of JB4375
JB4375Flag for United States of America

asked on

VB Script lists members of OU. Need it to query Group as well. List common members of both the group and OU.

I originally posted a this question with workable code to query the group. For those of you just joining the conversation, you can view this code at the top of this link:
https://www.experts-exchange.com/questions/23855076/Query-OU-in-Active-Directory-and-Return-a-Member-List.html

As the thread progressed, the OU portion of the problem proved to be a lot more difficult than I thought. For that reason we all agreed that we should continue with a related question. So where we are now is: The OU query is enclosed in an HTA file. A drop down exists to select the OU. After the OU is selected it displays the LDAP path. OU members are listed in the box below.

I want another drop down for the group selection. The Group LDAP path to be displayed the same as the OU, the common members of both the OU and the Group to be displayed in the box.  Current code to be used is attached.

Thanks,

JB
<Html>
<Head>
<Title>List OU Members</Title>
 
<HTA:Application
Caption = Yes
Border = Thick
ShowInTaskBar = Yes
SingleInstance = Yes
MaximizeButton = Yes
MinimizeButton = Yes>
 
<script Language = VBScript>
 
	Sub Window_OnLoad
		intWidth = 800
		intHeight = 600
		Me.ResizeTo intWidth, intHeight
		Me.MoveTo ((Screen.Width / 2) - (intWidth / 2)),((Screen.Height / 2) - (intHeight / 2))
		lst_members.Style.Width = 500
    	Set objRootDSE = GetObject("LDAP://RootDSE")
    	strBaseConnString = objRootDSE.Get("defaultNamingContext")
		Set objOULevel = GetObject("LDAP://" & strBaseConnString)
		RecurseOUs objOULevel, 0, strBaseConnString
		Show_Selection
	End Sub
 
	Sub Clear_Members
		For intListProgress = 1 To lst_members.Length
	   		lst_members.Remove 0
	   	Next
	End Sub
 
	Sub RecurseOUs(objOU, intLevel, strBaseConn)
		Dim objOUObject, strConnString, objActiveOption
		For Each objOUObject In objOU
			If UCase(Left(objOUObject.Name, 3)) = "OU=" Then
				strConnString = objOUObject.DistinguishedName
				Set objActiveOption = Document.CreateElement("OPTION")
		    	If intLevel = 0 Then
		    		objActiveOption.Text = Replace(objOUObject.Name, "OU=", "")
		    	Else
		    		objActiveOption.Text = String(intLevel * 4, " ") & "->   " & Replace(objOUObject.Name, "OU=", "")
		    	End If
		    	objActiveOption.Value = strConnString
		    	lst_SiteFilter.Add objActiveOption
				On Error Resume Next
				RecurseOUs GetObject("LDAP://" & strConnString), intLevel + 1, strBaseConn
				If Err.Number <> 0 Then
					MsgBox "Error enumerating " & strConnString
				End If
				Err.Clear
				On Error GoTo 0
			End If
		Next
	End Sub
 
	Sub Show_Selection
		span_SiteFilter.InnerHTML = lst_SiteFilter.Value
	End Sub
 
	Sub Default_Buttons
		If Window.Event.KeyCode = 13 Then
			btn_run.Click
		End If
	End Sub
 
	Sub Exit_HTA
		Window.Close
	End Sub
 
	Sub Get_Members
		Clear_Members
		strOU = lst_sitefilter.Value
		strLDAPPath = "LDAP://" & strOU
		
		Set objConnection2 = CreateObject("ADODB.Connection")
		Set objCommand2 = CreateObject("ADODB.Command")
		objConnection2.Provider = "ADsDSOObject"
		objConnection2.Open "Active Directory Provider"
		Set objCommand2.ActiveConnection = objConnection2
		
		Set objOU = GetObject(strLDAPPath)
		For Each objObject In objOU
			Set objMember = Document.CreateElement("OPTION")
			objMember.Text = objObject.cn
	        objMember.Value = objObject.cn
			lst_members.Add objMember, 0
		Next
	End Sub
</script>
<body style="background-color:#B0C4DE;" onkeypress='vbs:Default_Buttons'>
	<table height="90%" width= "90%" border="0" align="center">
		<tr>
			<td align="center" colspan="2">
				<h2>List OU Members</h2>
			</td>
		</tr>
		<tr>
			<td>
				<b>Site Filter:</b>
			</td>
			<td>
			    <select size='1' name='lst_SiteFilter'  onChange='vbs:Show_Selection'>
				</select>
			</td>
		</tr>
		<tr>
			<td colspan=2>
				<b>Site Selected:</b>&nbsp&nbsp&nbsp<span id='span_SiteFilter'></span>
			</td>
		</tr>
		<tr>
			<td>
				<b>Members:</b>
			</td>
			<td>
			    <select size='8' name='lst_members'>
				</select>
			</td>
		</tr>
	</table>
	<table width= "90%" border="0" align="center">
		<tr align="center">
			<td>
				<button name="btn_run" id="btn_run" accessKey="G" onclick="vbs:Get_Members"><u>G</u>et Members</button>
			</td>
			<td>
				<button name="btn_exit" id="btn_exit" accessKey="x" onclick="vbs:Exit_HTA">E<u>x</u>it</button>
			</td>
		</tr>
	</table>
</body>
</head>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JB4375

ASKER

Rob,
At first glance it looks great. It's 10:20 pm where I am. Currently remoting to do more server patches. Will test in the AM.
Thanks,
JB
OK, no problem.  Basically, what happens, is when you start it, it lists your OU structure in one list box, and an alphabetical list of all of the Groups in your AD.  Then, you select an OU, a group name, then click Get Members. It will then list each of the members that are in BOTH the OU *and* the Group that you have selected.

Regards,

Rob.
Avatar of JB4375

ASKER

Rob Sampson does it again. Thanks!!
Avatar of JB4375

ASKER

Rob,
It's PERFECT. What's more it a turn key solution anyone could use.
Thanks,
JB
Good to hear.  Thanks for the grade.

I was hoping I had the idea right when I was creating it... :-)

Regards,

Rob.