Sign in to follow this  
Followers 0
tbaror

list all comuter in active directory domain

5 posts in this topic

Hi,

I am looking for a script to display get list of all pc's in active drictory/domain using either ADSI NT or LDAP

thanks

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

This should look up the Default DSE and populate a listview with hosts. Any checked hosts are returned in an array.

#include <Array.au3>
#Include <GuiTreeView.au3>

BrowseHosts(True)

Func BrowseHosts($debug = False)
    $domain = GetRootDSE()
    If $domain = "Failed" Then 
        msgbox(0,"No Domain to Query","Not a member of a domain.")
        Return 
    EndIf
    $arrComputers = GetComputers($domain)
    $arrSelectedHosts = DispComputers($arrComputers)
    If $debug <> False Then _ArrayDisplay($arrSelectedHosts)
    Return $arrSelectedHosts
EndFunc

Func DispComputers($arrComputers)
    Dim $tTreeArr[1]
    $tGui = GUICreate("Browse for Hosts",250,326,-1,-1,"","")
    $tTree = GUICtrlCreateTreeView(5,5,235,258,BitOr(256,55))
    $tSelectAll = GUICtrlCreateButton("Select All",85,270,75,20)
    $tDeSelectAll = GUICtrlCreateButton("Deselect All",165,270,75,20)
    $tOKBtn = GUICtrlCreateButton("OK",5,270,75,20)
        GUICtrlSetState(-1,$GUI_FOCUS)
    
    For $i = 0 to UBound($arrComputers) - 1
        $tTreeArr[$i] = GUICtrlCreateTreeViewItem($arrComputers[$i],$tTree)
        ReDim $tTreeArr[$i + 2]
    Next
    
    $tVarTreeCount = _GUICtrlTreeView_GetCount(GUICtrlGetHandle($tTree))
    GUISetState(@SW_SHOW,$tGui)
    
    While 1
        
        $msg = GUIGetMsg()
        
        If $msg = $tSelectAll Then
            For $i = 0 to $tVarTreeCount - 1
                GUICtrlSetState($tTreeArr[$i],$GUI_CHECKED)
            Next
        EndIf
        
        If $msg = $tDeSelectAll Then
            For $i = 0 to $tVarTreeCount - 1
                GUICtrlSetState($tTreeArr[$i],$GUI_UNCHECKED)
            Next
        EndIf
        
        If $msg = $tOKBtn Then
            Local $tStrSelectedHosts
            For $i = 0 to $tVarTreeCount - 1
                If BitAnd(GUICtrlRead($tTreeArr[$i]),$GUI_CHECKED) Then
                    $tStrSelectedHosts = $tStrSelectedHosts & GUICtrlRead($tTreeArr[$i],1) & ","
                EndIf
            Next
            GUIDelete($tGui)
            $tArrSelectedHosts = StringSplit($tStrSelectedHosts,",")
            _ArrayDelete($tArrSelectedHosts,0)
            _ArrayDelete($tArrSelectedHosts,UBound($tArrSelectedHosts)-1)
            Return $tArrSelectedHosts
        EndIf
        
    WEnd
EndFunc

Func GetComputers($domainname)
    $objComputers = ObjGet("LDAP://CN=Computers," & $domainname)
    local $strComputers
    For $obj In $objComputers
        $strComputers = $strComputers & $obj.Name & ","
    Next
    $obj = ""
    $objComputers = ""
    $strComputers = StringLeft($strComputers,StringLen($strComputers)-1)
    $strComputers = StringReplace($strComputers,"CN=","")
    $arrComputers = StringSplit($strComputers,",")
    _ArrayDelete($arrComputers,0)
    _ArraySort($arrComputers)
    Return $arrComputers
EndFunc
    
Func GetRootDSE()
    $RootDSE = ObjGet("LDAP://RootDSE")
    If IsObj($RootDSE) Then
        Return $RootDSE.get( "DefaultNamingContext" )
    Else
        Return "Failed"
    EndIf
EndFunc
Edited by spudw2k

Share this post


Link to post
Share on other sites

This should look up the Default DSE and populate a listview with hosts. Any checked hosts are returned in an array.

Hi,

if anyone is interested in using spudw2k's very useful (at least for me, ADfunctions.au3 seems to be quite crappy) script, please be informed that in the code above an include for "GUIConstantsEx.au3" is missing. Please add the line:

#include <GUIConstantsEx.au3>

...to the top of the code.

Regards,

Chris

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