Sign in to follow this  
Followers 0
darkleton

AD.au3 and arrays in a treeview

13 posts in this topic

I'm trying to re-do the old ADTool (by Jonny Clelland) using the new AD.au3 by water.

I have got the first part of it, getting all the OUs, to display in a treeview, but whereas the old ADTool had them with a + next to it, mine just shows ALL ou's straight from the array.

How do i get the array to filter down.

For example mine shows:

Establishment

Establishment/WORK

Establishment/WORK/USERS

Where I want it to show Establishment, with an expand + sign next to it than then expands to the next level.

Is this possible? I have tried the $TVS_HASBUTTONS but I'm guessing some sort of formatting needs to be done to the array first?

This is the test code I am using right now:

_AD_Open()
$test = _AD_GetAllOUs()


$gui = GUICreate("TEST",800,600)
    GUISetBkColor("0xA0A0A0")
    $tree = GUICtrlCreateTreeView(5,5,399,590)
For $i = 1 To $test[0][0] 
GUICtrlCreateTreeViewItem($test[$i][0], $tree)
Next



    GUISetState(@SW_SHOW)
    
    While 1
        $msg = GUIGetMsg()

        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
        EndSelect
    WEnd

Thanks

Mike

Share this post


Link to post
Share on other sites



If anybody could help with this I'd be greatly appreciative.

I'm guessing it has something to do with the array and also using _guictrltreeview_expand but dont know how exactly. i've even copied the code directly from the old version and it doesn't work :(

Share this post


Link to post
Share on other sites

Hi,

this might helps you to build your treeview:

http://www.autoitscript.com/forum/index.php?showtopic=102547&st=0&p=727948&hl=joindomain&fromsearch=1&#entry727948

;-))

Stefan

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hi,

thanks for the link. unfortunately i'm just getting myself more and more lost :(

i haven't used the treeview functions like this before and i'm trying to learn as i go, and looking at that joindomain tool, it doesn't make as much sense as i'd hoped!

this is what i have so far, would you be able to give me pointers where i'm going wrong in this one?

#include <ad.au3>
#include <array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <Constants.au3>
#include <File.au3>
#include <GuiConstants.au3>
#Include <GuiTreeView.au3>
#include <TreeviewConstants.au3>
#include <GuiListBox.au3>
dim $test


_AD_Open()
$test = _AD_GetObjectsInOU("","(&(objectclass=organizationalUnit))", 1, "name,ADsPath", "name")

$gui = GUICreate("Test",800,600)
    GUISetBkColor("0xA0A0A0")
    $tree = GUICtrlCreateTreeView(5, 5, 399, 590, BitOR($GUI_SS_DEFAULT_TREEVIEW, $WS_BORDER), $WS_EX_CLIENTEDGE)
    $list = GUICtrlCreateList("", 410, 5, 380, 599, BitOR($LBS_SORT, $WS_BORDER, $WS_VSCROLL, $LBS_NOTIFY, $LBS_EXTENDEDSEL))

For $i = 1 To $test[0][0]
    GUICtrlCreateTreeViewItem($test[$i][0], $tree)
    GUICtrlSetOnEvent(-1, "UpdateTreeView")
Next
    GUISetState()
    Opt("GUIOnEventMode", 1)
    While 1
        $msg = GUIGetMsg()

        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
        EndSelect
    WEnd
    
Func UpdateTreeView()

    $parent = GUICtrlRead($tree)

    $curritem = $parent
    
    If BitAND(_GUICtrlTreeView_GetState($tree, $curritem), 64) = 64 Then
 
       UpdateList()

        Return
    EndIf
    $str_ldap = ""
    While GUICtrlRead(_GUICtrlTreeView_GetParentParam($tree, $curritem)) <> 0
        $str_ldap = $str_ldap & "ou=" & _GUICtrlTreeView_GetText($tree, $curritem) & ","

        $curritem = _GUICtrlTreeView_GetParentParam($tree, $curritem)
    WEnd
    $str_ldap = $str_ldap & "ou=" & _GUICtrlTreeView_GetText($Tree, $curritem) & ", dc=mydomain, dc=com"
    $test1 = _AD_GetObjectsInOU("","(&(objectclass=organizationalUnit))", 1, "name,ADsPath", "name")

    If $test1 <> 0 Then
        For $i = 1 To $test1[0][0]
            $currtreeitem = GUICtrlCreateTreeViewItem($test1[$i][0], @GUI_CtrlId)
            GUICtrlSetOnEvent($currtreeitem, "UpdateTreeView")
        Next
    EndIf
    _GUICtrlTreeView_Expand($tree, 1, $parent)
   
EndFunc   ;==>UpdateTreeView
what this one does at the moment is load the OUs in a treeview, but if i click on one of the OUs, it then reloads the whole OU list again Edited by darkleton

Share this post


Link to post
Share on other sites

Hi,

the program does what you code:

GUICtrlSetOnEvent($currtreeitem, "UpdateTreeView")

You have to check your logic about updating the treeview. You may work with a bool. If treeview is generated, you don't have to update the whole treeview, when you click an item.

;-))

Stefan

Share this post


Link to post
Share on other sites

Sorry, that was the code taken from the old ADTool.au3. Like i said, I'm a little bit lost with things like this. How would I go about making the tree not update but the child objects show correctly in it?

Thanks for being so patient :(

Share this post


Link to post
Share on other sites

Hi,

merging some code together based on adfunctions.au3.

Getting OU's and SubOU's into an listview; hopefully this is a starter for you:

#Include <GUIConstants.au3>
#Include <GuiListView.au3>
#include <WindowsConstants.au3>
#include <adfunctions.au3>
#include <Constants.au3>
#Include <GuiTreeView.au3>
#include <GUIConstantsEx.au3>
#include <array.au3>

Const $ouIdentifier     = "OU="
Const $cnIdentifier     = "CN="
Const $ouSeparator      = "\"

Global $gui = GUICreate("Test",800,600)
_filllistv ()
GUISetState()
While 1
    $msg = GUIGetMsg()

    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
    EndSelect
WEnd

Func _filllistv ()
    Dim $allOUsArray
    $arAllOU = _AdGetAllOUs ()
    _ArrayDelete ($arAllOU, 0)
    Global $defaultTreeView = GUICtrlCreateTreeView (5, 5, 399, 590, BitOR($GUI_SS_DEFAULT_TREEVIEW, $WS_BORDER), $WS_EX_CLIENTEDGE)
    ;------------------------------------------------------------------------------
    ; Reverse the order of all the OUs so we can do an easy alpabetical sort
    ; on the names
    ;------------------------------------------------------------------------------                     
    Dim $reversedAllOUsArray[1]
    For $ou in $arAllOU
        $reversedOU     = returnReversedOU($ou)
        $arrayAddStatus = _ArrayAdd($reversedAllOUsArray, $reversedOU)  
    Next

    ;------------------------------------------------------------------------------
    ; Sort the array alpabetically
    ;------------------------------------------------------------------------------
    $sortRevArrayStatus = _ArraySort($reversedAllOUsArray)      

    ;------------------------------------------------------------------------------
    ; Initialize our treeIDArray
    ; This will be used to look up the ID number of the GUI TreeView entries
    ; We need this to make nesting/child-parent relationships work.
    ;------------------------------------------------------------------------------
    Dim $treeIDArray[UBound($reversedAllOUsArray)*2]

    ;------------------------------------------------------------------------------
    ; Return the OUs to their original format and create the TreeView Entries.
    ;------------------------------------------------------------------------------
    $adCurrentObjectNumber = 0
    For $reversedOU in $arAllOU
        ;------------------------------------------------------------------------------
        ; Reverse the order back to the orginal format  
        ;------------------------------------------------------------------------------ 
        $ou = returnReversedOU($reversedOU)
        ;If StringInStr ($ou, "PGO") = 0 And StringInStr ($ou, "IE Zugriff") = 0 Then ContinueLoop
        ;------------------------------------------------------------------------------
        ; Calculate the additional values for this OU and check to see if its
        ; parentou has a GUI TreeID
        ;------------------------------------------------------------------------------     
        $fullOU         = StringStripWS($ou, 3)
        $friendlyOU     = returnFriendlyName($fullOU)           
        $parentFullOU   = returnParentOU($fullOU)           
        $parentTreeID   = _ArraySearch($treeIDArray, $parentFullOU)
        ;------------------------------------------------------------------------------
        ; Check if this OU has a parent. If it does make this TreeView item a child
        ; of that parent.
        ;------------------------------------------------------------------------------
        If ( $parentTreeID <> "-1" ) Then   
            $treeID = GUICtrlCreateTreeViewItem ($friendlyOU, $parentTreeID)
        Else   
            $treeID = GUICtrlCreateTreeViewItem ($friendlyOU, $defaultTreeView)
        EndIf   
        ;------------------------------------------------------------------------------
        ; The returned $treeID is sequential. We can use this to cheat and emulate
        ; a hashtable.
        ;------------------------------------------------------------------------------
        $treeIDArray[$treeID] = $fullOU
        $adCurrentObjectNumber = $adCurrentObjectNumber + 1
    Next
EndFunc

Func returnReversedOU($ou)
    $subOUArray = StringSplit($ou, $ouSeparator)
    $reversedOU = ""
    For $i = (UBound($subOUArray) - 1 ) To 1 Step -1 
        If $i = (UBound($subOUArray) -  1 ) Then
            $reversedOU = $reversedOU & $subOUArray[$i]
        Else
            $reversedOU = $reversedOU & $ouSeparator & $subOUArray[$i]
        EndIf
    Next
    return $reversedOU
EndFunc

;------------------------------------------------------------------------------
; Returns the friendly object name:
;
; Example:
;
; The full OU of:
; OU=IT Shop,OU=Service Groups,DC=domain,DC=com
;
; Is returned as:
; IT Shop
;------------------------------------------------------------------------------ 
Func returnFriendlyName($dn)
    return StringMid($dn, StringInStr($dn, "=")+1, StringInStr($dn, $ouSeparator)-(StringInStr($dn, "=")+1))
EndFunc

;------------------------------------------------------------------------------
; Returns the full OU name from a DN record:
;
; Example:
;
; The DN:
; CN=Paula,OU=IT Shop,OU=Service Groups,DC=domain,DC=com
;
; Is returned as:
; OU=IT Shop,OU=Service Groups,DC=domain,DC=com
;------------------------------------------------------------------------------ 
Func returnFullOUfromDN($dn)
    If $dn <> "0" Then
        return StringRight($dn, StringLen($dn)-StringInStr($dn, $ouIdentifier)+1)
    Else
        return 0
    EndIf
EndFunc

;------------------------------------------------------------------------------
; Returns the parent OU name:
;
; Example:
;
; The full OU of:
; OU=IT Shop,OU=Service Groups,DC=domain,DC=com
;
; Is returned as:
; OU=Service Groups,DC=domain,DC=com
;------------------------------------------------------------------------------ 
Func returnParentOU($fullOU)
    return StringRight($fullOU, ( StringLen($fullOU)-StringInStr($fullOU, $ouSeparator) ) )
EndFunc

AD_Functions as attachment:

adfunctions.au3

;-))

Stefan

Share this post


Link to post
Share on other sites

thank you very much :(

it appears my problem is now with the new AD.au3 which im using which replaces adfunctions.au3

if i have the old adfunctions.au3 it will return the OUs fine. If I use the new AD.au3, and display the array, there is an extra column.

old one has

[0] OU1

[1] OU2

etc, where as using the new AD.au3 i get

[0] OU1 OU=OU1,DC=mydmomain,DC=com

and it seems because of that it then fails to show in the treeview. How can i delete a column from an array?

Sorry for so many questions!!

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Hi,

heres the function _filllistv based on ad.au3.

Change #include <adfunctions.au3> to #include <ad.au3> and replace function.

There are changes in the For..Loops to adress the first element in array:

Func _filllistv ()
    Dim $allOUsArray
    _ad_open ()
    $arAllOU = _Ad_GetAllOUs ()
    _ad_close ()
    _ArrayDisplay ($arAllOU)
    _ArrayDelete ($arAllOU, 0)
    Global $defaultTreeView = GUICtrlCreateTreeView (5, 5, 399, 590, BitOR($GUI_SS_DEFAULT_TREEVIEW, $WS_BORDER), $WS_EX_CLIENTEDGE)
    ;------------------------------------------------------------------------------
    ; Reverse the order of all the OUs so we can do an easy alpabetical sort
    ; on the names
    ;------------------------------------------------------------------------------                     
    Dim $reversedAllOUsArray[1]
    For $i = 0 To UBound ($arAllOU, 1) - 1
        Consolewrite ($arAllOU [$i] [0])
        $reversedOU     = returnReversedOU($arAllOU [$i] [0])
        $arrayAddStatus = _ArrayAdd($reversedAllOUsArray, $reversedOU)  
    Next

    ;------------------------------------------------------------------------------
    ; Sort the array alpabetically
    ;------------------------------------------------------------------------------
    $sortRevArrayStatus = _ArraySort($reversedAllOUsArray)      

    ;------------------------------------------------------------------------------
    ; Initialize our treeIDArray
    ; This will be used to look up the ID number of the GUI TreeView entries
    ; We need this to make nesting/child-parent relationships work.
    ;------------------------------------------------------------------------------
    Dim $treeIDArray[UBound($reversedAllOUsArray)*2]

    ;------------------------------------------------------------------------------
    ; Return the OUs to their original format and create the TreeView Entries.
    ;------------------------------------------------------------------------------
    $adCurrentObjectNumber = 0
    For $i = 0 To UBound ($arAllOU) - 1
        ;------------------------------------------------------------------------------
        ; Reverse the order back to the orginal format  
        ;------------------------------------------------------------------------------ 
        $ou = returnReversedOU($arAllOU [$i] [0])
        ;If StringInStr ($ou, "PGO") = 0 And StringInStr ($ou, "IE Zugriff") = 0 Then ContinueLoop
        ;------------------------------------------------------------------------------
        ; Calculate the additional values for this OU and check to see if its
        ; parentou has a GUI TreeID
        ;------------------------------------------------------------------------------     
        $fullOU         = StringStripWS($ou, 3)
        $friendlyOU     = returnFriendlyName($fullOU)           
        $parentFullOU   = returnParentOU($fullOU)           
        $parentTreeID   = _ArraySearch($treeIDArray, $parentFullOU)
        ;------------------------------------------------------------------------------
        ; Check if this OU has a parent. If it does make this TreeView item a child
        ; of that parent.
        ;------------------------------------------------------------------------------
        If ( $parentTreeID <> "-1" ) Then   
            $treeID = GUICtrlCreateTreeViewItem ($friendlyOU, $parentTreeID)
        Else   
            $treeID = GUICtrlCreateTreeViewItem ($friendlyOU, $defaultTreeView)
        EndIf   
        ;------------------------------------------------------------------------------
        ; The returned $treeID is sequential. We can use this to cheat and emulate
        ; a hashtable.
        ;------------------------------------------------------------------------------
        $treeIDArray[$treeID] = $fullOU
        $adCurrentObjectNumber = $adCurrentObjectNumber + 1
    Next
EndFunc

;-))

Stefan

Edited by 99ojo

Share this post


Link to post
Share on other sites

thank you so much, that is fantastic :(

Share this post


Link to post
Share on other sites

Glad to see that my post is useful for some one :(

Share this post


Link to post
Share on other sites

was the domin join tool yours? thank you for that too :(

Share this post


Link to post
Share on other sites

Hi,

thanks for the link. unfortunately i'm just getting myself more and more lost :blink:

i haven't used the treeview functions like this before and i'm trying to learn as i go, and looking at that joindomain tool, it doesn't make as much sense as i'd hoped!

this is what i have so far, would you be able to give me pointers where i'm going wrong in this one?

#include <ad.au3>
#include <array.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <Constants.au3>
#include <File.au3>
#include <GuiConstants.au3>
#Include <GuiTreeView.au3>
#include <TreeviewConstants.au3>
#include <GuiListBox.au3>
dim $test


_AD_Open()
$test = _AD_GetObjectsInOU("","(&(objectclass=organizationalUnit))", 1, "name,ADsPath", "name")

$gui = GUICreate("Test",800,600)
    GUISetBkColor("0xA0A0A0")
    $tree = GUICtrlCreateTreeView(5, 5, 399, 590, BitOR($GUI_SS_DEFAULT_TREEVIEW, $WS_BORDER), $WS_EX_CLIENTEDGE)
    $list = GUICtrlCreateList("", 410, 5, 380, 599, BitOR($LBS_SORT, $WS_BORDER, $WS_VSCROLL, $LBS_NOTIFY, $LBS_EXTENDEDSEL))

For $i = 1 To $test[0][0]
    GUICtrlCreateTreeViewItem($test[$i][0], $tree)
    GUICtrlSetOnEvent(-1, "UpdateTreeView")
Next
    GUISetState()
    Opt("GUIOnEventMode", 1)
    While 1
        $msg = GUIGetMsg()

        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
        EndSelect
    WEnd
    
Func UpdateTreeView()

    $parent = GUICtrlRead($tree)

    $curritem = $parent
    
    If BitAND(_GUICtrlTreeView_GetState($tree, $curritem), 64) = 64 Then
 
       UpdateList()

        Return
    EndIf
    $str_ldap = ""
    While GUICtrlRead(_GUICtrlTreeView_GetParentParam($tree, $curritem)) <> 0
        $str_ldap = $str_ldap & "ou=" & _GUICtrlTreeView_GetText($tree, $curritem) & ","

        $curritem = _GUICtrlTreeView_GetParentParam($tree, $curritem)
    WEnd
    $str_ldap = $str_ldap & "ou=" & _GUICtrlTreeView_GetText($Tree, $curritem) & ", dc=mydomain, dc=com"
    $test1 = _AD_GetObjectsInOU("","(&(objectclass=organizationalUnit))", 1, "name,ADsPath", "name")

    If $test1 <> 0 Then
        For $i = 1 To $test1[0][0]
            $currtreeitem = GUICtrlCreateTreeViewItem($test1[$i][0], @GUI_CtrlId)
            GUICtrlSetOnEvent($currtreeitem, "UpdateTreeView")
        Next
    EndIf
    _GUICtrlTreeView_Expand($tree, 1, $parent)
   
EndFunc   ;==>UpdateTreeView
what this one does at the moment is load the OUs in a treeview, but if i click on one of the OUs, it then reloads the whole OU list again

Here's a quick script I wrote to pull all the OU's from AD then display them in Treeview. You'll have to take this one step further and populate all the OU's with their contents (Users, Groups, and Computers). Give it a try and hopefully this will help in what you're trying to do.

(See attached for script)

GTIFSI

LDAP Test.au3

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  
Followers 0