I'm working on a user.hta file and I need to populate a pull down of all
the users, then with that I can populate all the other information
fields and eventually get the hta to use POST to update specific
information.

Anyone have an example of using some ADSI within an hta ?


Mike

Re: Populating a pull down list in a .hta from AD by Torgeir

Torgeir
Thu Jan 06 09:42:41 CST 2005

Mike Brierley wrote:

> I'm working on a user.hta file and I need to populate a pull down of all
> the users, then with that I can populate all the other information
> fields and eventually get the hta to use POST to update specific
> information.
>
> Anyone have an example of using some ADSI within an hta ?
Hi

Attached below is a HTA I have created (put it in a file with .HTA file
extension and double click on it) that will amongst other things list
all groups a user is member of directly as well as all the indirect
groups (including the group description)

It will give this output if you tick off for "Include groups":

' *********** Output start ***********
Properties for D123456

-------------------------------------------------------

-------------------------------------------------------

General information

Displayname: Torgeir Bakken
Home Directory: \\MY01SERVER\Users\D123456
Office Location: NO-POR-TST
Description: SIS / Torgeir Bakken


-------------------------------------------------------

Group membership (Description / Group Name)

Description Group1 (Group1) <--- Direct group
----->Description Group2 (Group2) <--- Nested group

Description Group3 (Group3)
----->Description Group4 (Group4)

Description Group5 (Group5)
----->Description Group6 (Group6)
----->Description Group7 (Group7)

' *********** Output stop ***********


' *********** HTA start ***********

<html>

<head>

<hta:application id='oHTAUser'
applicationName='UserInfo'
border='thick'
borderstyle='normal'
caption='yes'
icon=''
maximizebutton='yes'
minimizebutton='yes'
showintaskbar='yes'
singleinstance='no'
sysmenu='yes'
version='1.0'
windowstate='normal'

<style>
body,table {font:10pt verdana}
</style>

<!-- Script --------------------------->

<script language='vbscript'>
' Author: Torgeir Bakken
Option Explicit

Dim dicGroupList, dicGroupDirectList

Sub window_onload()
document.title = oHTAUser.applicationName
idUserObject.focus
End Sub

Sub UserInfo()
Dim separator
separator = "<hr>"
clr

Dim sUserID, sAdsPath, oUser, sGroup, sSubGroup, oGroup, oSubGroup
Dim iNrGroups, iUserControlFlags

sUserID = idUserObject.value

If sUserID = "" Then
Exit Sub
End If

sAdsPath = GetLdapDN(sUserID)

If sAdsPath = "" Then
w "<br>No user found! <br><br><hr>"
Exit Sub
End If

w "Ctrf+F for Find, Ctrl+P for Print, " _
& "Ctrl+End to goto end, Ctrl+Home to goto top<br><br>"

w "Properties for " & sUserID & "<br>"
w "<hr><hr>"

Set oUser = GetObject("LDAP://" & sAdsPath)

w "<b>General information</b><br><br>"

iUserControlFlags = oUser.userAccountControl
If (iUserControlFlags And &H2) <> 0 Then
w "<b>Account is disabled!<br></b>"
End If

w "Displayname: " & oUser.displayName & "<br>"
w "Home Directory: " & oUser.homeDirectory & "<br>"
w "Office Location: " & oUser.physicalDeliveryOfficeName & "<br>"
w "Description: " & oUser.Description & "<br>"
w "<br>"
w separator

If (idIncludeGroups.checked) Then
w "<b>Group membership (Description / Group Name)</b><br>"

Set dicGroupDirectList = CreateObject("Scripting.Dictionary")
LoadDirectGroups(oUser)
' number of direct groups: dicGroupDirectList.Count
iNrGroups = 0
For Each sGroup In dicGroupDirectList
iNrGroups = iNrGroups + 1

' get AdsPath for group (using dict. item)
' and connect to the group.
Set oGroup = GetObject(dicGroupDirectList(sGroup))

w "<br>"
w oGroup.Description & " (" & sGroup & ")<br>"

Set dicGroupList = CreateObject("Scripting.Dictionary")
LoadGroups(oGroup)

For Each sSubGroup In dicGroupList
iNrGroups = iNrGroups + 1

' get AdsPath for group (using dict. item)
' and connect to the group.
Set oSubGroup = GetObject(dicGroupList(sSubGroup))

w "----->"& oSubGroup.Description & " (" & sSubGroup & ")<br>"

Next
Next

w "<br>Total # of groups: " & iNrGroups & "<br>"
w "<br>"
w separator

End If

w "Finished at:" & now()
w "</body></html>"

End Sub

Sub clr()
idOutput.innerHtml = ""
End Sub

Sub w(str)
idOutput.insertAdjacentHtml "beforeend", str
End Sub

'*********************************************************
' Function : GetLdapDN
' Purpose: Returns LDAP Distinguished Name from domain\user query
'*********************************************************
Function GetLdapDN(sNTName)
Dim oRoot, oTrans, sDNSDomain, sNetBIOSDomain
Set oRoot = GetObject("LDAP://RootDSE")
Set oTrans = CreateObject("NameTranslate")
sDNSDomain = oRoot.Get("DefaultNamingContext")
oTrans.Init 3, sDNSDomain
oTrans.Set 1, sDNSDomain
sNetBIOSDomain = oTrans.Get(3)
oTrans.Init 1, Left(sNetBIOSDomain, Len(sNetBIOSDomain) - 1)
On Error Resume Next
oTrans.Set 3, sNetBIOSDomain & sNTName
If Err.Number <> 0 Then
GetLdapDN = ""
Else
GetLdapDN = oTrans.Get(1)
End If
End Function

'*********************************************************
' Sub : LoadGroups
' Purpose: Returns all groups an object is member of
' directly or indirectly
'*********************************************************
Sub LoadGroups(oADObject)
' Recursive sub, populate dictionary object with groups.
Dim sGroups, oGroup, j
dicGroupList.CompareMode = vbTextCompare
sGroups = oADObject.MemberOf
If IsEmpty(sGroups) Then
Exit Sub
End If
If TypeName(sGroups) = "String" Then
Set oGroup = GetObject("LDAP://" & sGroups)
If Not dicGroupList.Exists(oGroup.sAMAccountName) Then
dicGroupList(oGroup.sAMAccountName) = oGroup.AdsPath
Call LoadGroups(oGroup)
End If
Set oGroup = Nothing
Exit Sub
End If
For j = 0 To UBound(sGroups)
Set oGroup = GetObject("LDAP://" & sGroups(j))
If Not dicGroupList.Exists(oGroup.sAMAccountName) Then
dicGroupList(oGroup.sAMAccountName) = oGroup.AdsPath
Call LoadGroups(oGroup)
End If
Next
Set oGroup = Nothing
End Sub

'*********************************************************
' Sub : LoadDirectGroups
' Purpose: Returns groups the user is member of directly
' Dict. key is sAMAccountName and dict. item is AdsPath
'*********************************************************
Sub LoadDirectGroups(oADObject)
Dim oGroup
For Each oGroup In oADObject.Groups
dicGroupDirectList(oGroup.sAMAccountName) = oGroup.AdsPath
'WScript.Echo oGroup.AdsPath
Next
Set oGroup = Nothing
End Sub

</script>

<!-- End Script ----------------------->

</head>

<!-- Body -->

<body id="idBody">

<!-- UI Area -------------------------->

<DIV id='idMain'>

<input type=text' id='idUserObject' size='30'>&nbsp;

<input type='button' onclick='UserInfo()' value='Enter User ID'>

<input type='checkbox' id='idIncludeGroups' <p>Include groups

<hr>

</DIV>

<!-- Output Area ---------------------->

<DIV id='idOutput'></DIV>

</body>
</html>


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx