Jump to content
Sign in to follow this  
nhalme

How Can I List All the Groups in an OU in a List control

Recommended Posts

nhalme

Hi,

Im trying to list all the groups in an OU in a list control (GUICtrlCreateList).

The idea is taken from "Hey, Scripting Guy! How Can I List All the Groups in an OU?"

And the idea is done in HTA like this:

<html>
<head>
<title>List All the Groups in an OU</title>
<HTA:APPLICATION 
     ID="HTA"
     APPLICATIONNAME="ADGroups"
     SCROLL="yes"
     SINGLEINSTANCE="yes"
     WINDOWSTATE="normal"
>
</head>
<script Language="VBScript">
    Sub Window_onload
        Set objOU = GetObject("LDAP://ou=Security Groups,ou=Managed Objects,dc=contoso,dc=com")
        ObjOU.Filter= Array("Group")
        For Each objGroup in objOU
          Set objOption = document.createElement("OPTION")
          objOption.Text = objGroup.Name
          objOption.Value = objGroup.Name
          AppsGroups.Add(objOption)
        Next
    End Sub
</SCRIPT>
<body>
    <select size="5" name="AppsGroups"></select>
</body>
</html>

So Im trying to do the same with AutoIT. I want to use list control (GUICtrlCreateList) to display the results. I can get the results to a txt file:

#include <Array.au3>

Local $colCroups, $sTmp, $Array[1] = ["Group"]
$colGroups = ObjGet("LDAP://ou=Security Groups,ou=Managed Objects,dc=contoso,dc=com")
$colGroups.Filter = $Array
$file = FileOpen("test.txt", 1)
; Check if file opened for writing OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
For $objGroup In $colGroups
    $sTmp = $objGroup.Name & @CRLF
    FileWrite($file, $sTmp)
Next
FileClose($file)

or display it in an array (thanks to smashly):

#include <Array.au3>
$ListGroups = _ListGroups()
If Not @error Then _ArrayDisplay($ListGroups, "List Groups")

Func _ListGroups()
    Local $colCroups, $sTmp, $Array[1] = ["Group"]
    $colGroups = ObjGet("LDAP://ou=Security Groups,ou=Managed Objects,dc=contoso,dc=com")
    If Not IsObj($colGroups) Then Return SetError(1, 0, 0)
    $colGroups.Filter = $Array
    For $objGroup In $colGroups
        $sTmp &= $objGroup.Name & "|"
    Next
    Return SetError(0, 0, StringSplit(StringTrimRight($sTmp, 1), "|"))
EndFunc  ;==>_ListGroups

but not in a list control. Can someone help me? Should not that hard, but I just cannot solve this by myself.

Here is one of my tests to get it work:

#include <Array.au3>    

$MainGUI=GUICreate("List AD Groups", 220, 250, 100, 200)
Global $colCroups, $sTmp, $Array[1] = ["Group"]
$colGroups = ObjGet("LDAP://ou=Security Groups,ou=Managed Objects,dc=contoso,dc=com")
$colGroups.Filter = $Array
For $objGroup In $colGroups
    $sTmp = $objGroup.Name & "|"
Next
$ListGroups =StringSplit(StringTrimRight($sTmp, 1), "|")
$list = GUICtrlCreateList($ListGroups, 10, 10, 200, 150)
;********************************************************************************
;   Display the Main GUI
;********************************************************************************
GUISetState(@SW_SHOW); will display the dialog box
While 1
    Sleep(1000); Idle around
WEnd

Share this post


Link to post
Share on other sites
GEOSoft

Just modifying your array method should do it.

;#include <Array.au3>
$List = GUICtrlCreateList("", 10, 10 ,100, 150)
$ListGroups = _ListGroups()
If Not @error Then GUICtrlSetData($List, "|" & $ListGroups)

Func _ListGroups()
    Local $colCroups, $sTmp, $Array[1] = ["Group"]
    $colGroups = ObjGet("LDAP://ou=Security Groups,ou=Managed Objects,dc=contoso,dc=com")
    If Not IsObj($colGroups) Then Return SetError(1, 0, 0)
    $colGroups.Filter = $Array
    For $objGroup In $colGroups
        $sTmp &= $objGroup.Name & "|"
    Next
  ;Return SetError(0, 0, StringSplit(StringTrimRight($sTmp, 1), "|"))
    Return SetError(0, 0, StringTrimRight($sTmp, 1))
EndFunc;==>_ListGroups
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
GEOSoft

Thanks GEOSoft! Your advice solved the problem.

NP

Here is a slightly modified version that is probably better.

$List = GUICtrlCreateList("", 10, 10 ,100, 150)
$ListGroups = _ListGroups()
If Not @error Then GUICtrlSetData($List, $ListGroups)

Func _ListGroups()
    Local $colCroups, $sTmp, $Array[1] = ["Group"], $sSep = Opt("GUIDataSeparatorChar")
    $colGroups = ObjGet("LDAP://ou=Security Groups,ou=Managed Objects,dc=contoso,dc=com")
    If Not IsObj($colGroups) Then Return SetError(1, 0, 0)
    $colGroups.Filter = $Array
    For $objGroup In $colGroups
      ;$sTmp &= $objGroup.Name & "|"
        $sTmp &= $sSep & $objGroup.Name
    Next
  ;Return SetError(0, 0, StringTrimRight($sTmp, 1))
    Return SetError(0, 0, $sTmp)
EndFunc;==>_ListGroups
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×