Sign in to follow this  
Followers 0
mheikal

List computers accounts on Active Directory OU

25 posts in this topic

Hi Guys

I need your help to find script that list computer accounts on Computers OU in active directory and display for each computer its full name , operating system and ownership (the person who joined this computer to domain)

Appreciate your help

Share this post


Link to post
Share on other sites



Hi Guys

I need your help to find script that list computer accounts on Computers OU in active directory and display for each computer its full name , operating system and ownership (the person who joined this computer to domain)

Appreciate your help

Hi mheikal,

there is an UDF (adfunctions.au3) available to do what you want. Or you can use the beta version of my AD.au3 which is based on the adfunctions.au3.

To get a list of all computers with full name, operating system and ownership I would use:

#include <ADfunctions.au3>

Global $aComputers
$sOU = $strDNSDomain
_ADGetObjectsInOU($aComputers,$sOU,"(objectclass=computer)",2,"name,operatingSystem")
_ArrayDisplay($aComputers)

I'm not sure what to query to get the ownership but the script should give you a good starting point.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hi mheikal,

there is an UDF (adfunctions.au3) available to do what you want. Or you can use the beta version of my AD.au3 which is based on the adfunctions.au3.

To get a list of all computers with full name, operating system and ownership I would use:

#include <ADfunctions.au3>

Global $aComputers
$sOU = $strDNSDomain
_ADGetObjectsInOU($aComputers,$sOU,"(objectclass=computer)",2,"name,operatingSystem")
_ArrayDisplay($aComputers)

I'm not sure what to query to get the ownership but the script should give you a good starting point.

Hi water,

How are you? is that not possible to do it your script _ADGetObjectsInOU() ?


Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

Share this post


Link to post
Share on other sites

Hi Syed,

thanks I'm fine and I hope you are fine too.

This thread was opened at a time when my AD UDF was just a beta. NOW everything you can do with adfunctions.au3 can be done using my AD.au3 - and more.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

i am fine too,Thanks! I am extremely sorry for being childish do you have any example how to find computer names in particular OU ?


Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

Share this post


Link to post
Share on other sites

This should work with my AD UDF:

#include <AD.au3>

Global $aComputers
Global $sOU = "enter the FQDN of the OU where to start here"
$aComputers = _AD_GetObjectsInOU($sOU, "(objectclass=computer)", 2, "name,samaccountname")
_ArrayDisplay($aComputers)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Hi Water,

Thanks for the quick help, but it returns error value 3.

Even i tried hardcoding the OU name as "OU=sitename,OU=country,OU=region,OU=Accounts,DC=xxx,DC=com"

but still it returns the value 3. Tried with #requireadmin as well


Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

Share this post


Link to post
Share on other sites

Error 3 means: No records found. I added _AD_Open and the following script works fine here:

#include <AD.au3>

Global $aComputers
Global $sOU = ""
_AD_Open()
$aComputers = _AD_GetObjectsInOU($sOU, "(objectclass=computer)", 2, "name,samaccountname")
_AD_Close()
_ArrayDisplay($aComputers)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
:( i tried that already here it is not working :

Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

Share this post


Link to post
Share on other sites

You are sure the computer accounts are members of the specified OU?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
:D you got me! OU name differs... for user accounts it has the ou as accounts and for computers it is having as computers...

Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

Share this post


Link to post
Share on other sites

Glad it was that simple :D


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thank you so much Water! just curious, could you please helpe me to understand why the below example script return only OU related to Accounts not related to computer? what i have to do if i want the OR details which is related to computer?

#AutoIt3Wrapper_AU3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=Y
#include <AD.au3>

; Open Connection to the Active Directory
_AD_Open()
If @error Then Exit MsgBox(16, "Active Directory Example Skript", "Function _AD_Open encountered a problem. @error = " & @error & ", @extended = " & @extended)

; *****************************************************************************
; Example 1
; Get a list of all OUs in the Active Directory
; *****************************************************************************
Global $aOUs = _AD_GetAllOUs()
If @error > 0 Then
MsgBox(64, "Active Directory Functions - Example 1", "No OUs could be found")
Else
_ArrayDisplay($aOUs, "Active Directory Functions - Example 1 - All OUs found in the Active Directory")
EndIf
_AD_close()

Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

Share this post


Link to post
Share on other sites

As you didn't pass any parameters _AD_GetAllOUs should return an array of ALL OUs in your AD.

Could you try example 3 from the _AD_GetAllOUs.au3 example script?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

yeah.. both of them works but i am trying with options which strikes on mind but i am not able to get the OU details only contains Workstations :(


Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

Share this post


Link to post
Share on other sites

The function only lets you specify a starting OU and then lists all OUs below. You can't get all OUs where there are computer accounts.

You could have a look at the _AD_Example_GetOUTreeView example script. It allows to specify

Set parameters $bAD_Count = True and $sAD_Category = computer and the count of computers in an OU will be displayed.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Hi Water,

i tried the example which you said and i tried to add the line below for $OU variable where i get an error message below. Not able to understand the reason :(

;------------------------------
; Make your changes below
;------------------------------
Global $sOU = _AD_SamAccountNameToFQDN()
Global $iPos = StringInStr($sOU, ",")
 $sOU = StringMid($sOU, $iPos + 1)

C:Program Files (x86)AutoIt3IncludeAD.au3 (455) : ==> Variable must be of type "Object".:
$__oAD_Command.CommandText = "<LDAP://" &amp; $sAD_HostServer &amp; "/" &amp; $sAD_DNSDomain &amp; ">;(sAMAccountName=" &amp; $sAD_SamAccountName &amp; ");distinguishedName;subtree"
$__oAD_Command^ ERROR
->10:39:51 AutoIT3.exe ended.rc:1
Edited by Syed23

Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

You have to pass an account name for which you want the FQDN. To get the FQDN for your user account you would use:

Global $sOU = _AD_SamAccountNameToFQDN(@UserName)
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

To get the OU where your computer is a member of you need the following code:

Global $sOU = _AD_SamAccountNameToFQDN(@Computername & "$")
Global $iPos = StringInStr($sOU, ",")
$sOU = StringMid($sOU, $iPos + 1)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

: was it some thing wrong on the code below? not sure why i am getting again and again same error message.

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=n
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <AD.au3>
#include <TreeviewConstants.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GuiTreeView.au3>

Global $sTitle = "Active Direcory OU Treeview"
#region ### START Koda GUI section ### Form=
Global $hMain = GUICreate($sTitle, 743, 683, -1, -1)
Global $hTree = GUICtrlCreateTreeView(6, 6, 600, 666, -1, $WS_EX_CLIENTEDGE)
Global $bExit = GUICtrlCreateButton("Exit", 624, 8, 97, 33)
Global $bExpand = GUICtrlCreateButton("Expand", 624, 56, 97, 33)
Global $bCollapse = GUICtrlCreateButton("Collapse", 624, 104, 97, 33)
Global $bSelect = GUICtrlCreateButton("Select OU", 624, 152, 97, 33)
#endregion ### END Koda GUI section ###

;------------------------------
; Make your changes below
;------------------------------
Global $sOU = _AD_SamAccountNameToFQDN(@Computername & "$")
Global $iPos = StringInStr($sOU, ",")
$sOU = StringMid($sOU, $iPos + 1)            ; FQDN of the OU where to start
Global $sCategory = "computer"      ; Category to select when counts should be displayed or full LDAP query
Global $sText = " (Computer: %)"    ; Text to use for the counts to display. % is replaced with the count
Global $iScope = 1                  ; Scope for the LDAP search to calculate the count. 1 = Only the OU, 2 = OU + sub-OUs
Global $bCount = True               ; 1 = Display the object count right to the OU
Global $bDisplay = True             ; True = Display objects in the OU as well (selection = $sCategory)
;------------------------------
; Make your changes above
;------------------------------
Global $aTreeView = _AD_GetOUTreeView($sOU, $hTree, False, $bCount, $bDisplay, $sCategory, $sText, $iScope)
If @error <> 0 Then MsgBox(16, "Active Direcory OU Treeview", "Error creating list of OUs starting with '" & $sOU & "'." & @CRLF & _
"Error returned by function _AD_GetALLOUs: @error = " & @error & ", @extended =  " & @extended)
GUISetState(@SW_SHOW)

While 1
$Msg = GUIGetMsg()
Switch $Msg
Case $GUI_EVENT_CLOSE, $bExit
Exit
Case $bExpand
_GUICtrlTreeView_Expand($hTree)
Case $bCollapse
_GUICtrlTreeView_Expand($hTree, 0, False)
Case $bSelect
$hSelection = _GUICtrlTreeView_GetSelection($hTree)
$sSelection = _GUICtrlTreeView_GetText($hTree, $hSelection)
For $i = 1 To $aTreeView[0][0]
If $hSelection = $aTreeView[$i][2] Then ExitLoop
Next
$sOU = $aTreeView[$i][1]
MsgBox(64, $sTitle & " - Selected OU", "Name: " & $sSelection & @CRLF & "FQDN: " & $sOU)
EndSwitch
WEnd

; #FUNCTION# ====================================================================================================================
; Name...........: _AD_GetOUTreeView
; Description ...: Fills a given TreeView control with all OUs starting with a given OU.
; Syntax.........: _AD_GetOUTreeView($sAD_OU, $hAD_TreeView[, $bAD_IsADOpen = False[, $bAD_Count = False[, $bAD_Display = False[, $sAD_Category = ""[, $sAD_Text = " (%)"[, $iAD_SearchScope = 1]]]]]])
; Parameters ....: $sAD_OU          - FQDN of the OU where to start. "" displays all OUs
;                  $hAD_TreeView    - Handle of the TreeView where the OUs will be displayed. Has to be created in advance
;                  $bAD_IsADOpen    - Optional: Pass as True if the connection to the AD has already been made by the calling script (default = False)
;                  $bAD_Count       - Optional: True will display the count of objects in the OU right to the OU name (default = False)
;                                     The LDAP query to determine the count is built from $sAD_Category
;                  $bAD_Display     - Optional: True will display the objects in the OU below the OU itself (default = False)
;                  $sAD_Category    - Optional: Category to search for or the complete LDAP search string e.g. "computer" and "(objectcategory=computer)" are equivalent
;                                     The number of found objects is added to the OUs name (default = "" = don't count objects)
;                  $sAD_Text        - Optional: Text to display the count. Must contain a % which will be replaced by the actual number (default = " (%)")
;                  $iAD_SearchScope - Optional: Search scope for function _AD_GetObjectsInOU: 0 = base, 1 = one-level, 2 = sub-tree (default = 1)
; Return values .: Success - 1
;                  Failure - Returns 0 and sets @error:
;                  |x - Function _AD_Open or _AD_GetAllOUs failed. @error and @extended are set by _AD_Open or _AD_GetAllOUs
; Author ........: water bnased on code by ReFran - http://www.autoitscript.com/forum/topic/84119-treeview-read-to-from-file
; Modified.......: water including ideas by HaeMHuK
; Remarks .......: Use $iAD_SearchScope = 1 to get the number of objects of a single OU.
;                  Use $iAD_SearchScope = 2 to get the number of objects in the OU plus all sub-OUs.
; Related .......:
; Link ..........:
; Example .......:
; ===============================================================================================================================
Func _AD_GetOUTreeView($sAD_OU, $hAD_TreeView, $bAD_IsADOpen = False, $bAD_Count = False, $bAD_Display = False, $sAD_Category = "", $sAD_Text = " (%)", $iAD_SearchScope = 1)

Local $iAD_Count, $aObjects
If $bAD_IsADOpen = False Then
_AD_Open()
If @error Then Return SetError(@error, @extended, 0)
EndIf
$sSeparator = ""
Local $aAD_OUs = _AD_GetAllOUs($sAD_OU, $sSeparator)
If @error <> 0 Then Return SetError(@error, @extended, 0)
Local $aAD_TreeView[$aAD_OUs[0][0] + 1][3] = [[$aAD_OUs[0][0], 3]]
Local $aAD_Result = $aAD_TreeView
For $i = 1 To $aAD_OUs[0][0]
$aAD_Temp = StringSplit($aAD_OUs[$i][0], $sSeparator)
$aAD_TreeView[$i][0] = StringFormat("%" & $aAD_Temp[0] - 1 & "s", "") & "#" & $aAD_Temp[$aAD_Temp[0]]
$aAD_TreeView[$i][1] = $aAD_OUs[$i][1]
Next

_GUICtrlTreeView_BeginUpdate($hAD_TreeView)
Local $ahAD_Node[50], $sAD_LDAPString
If $sAD_Category <> "" And StringIsAlNum($sAD_Category) Then
$sAD_LDAPString = "(objectcategory=" & $sAD_Category & ")"
Else
$sAD_LDAPString = $sAD_Category
EndIf
$iAD_OutIndex = 0
For $iAD_Index = 1 To $aAD_TreeView[0][0]
$iAD_OutIndex += 1
$aObjects = ""
$sAD_Line = StringSplit(StringStripCR($aAD_TreeView[$iAD_Index][0]), @TAB)
$iAD_Level = StringInStr($sAD_Line[1], "#")
If $iAD_Level = 0 Then ExitLoop
If ($bAD_Count Or $bAD_Display) And $sAD_LDAPString <> "" Then
If $bAD_Display Then
$aObjects = _AD_GetObjectsInOU($aAD_TreeView[$iAD_Index][1], $sAD_LDAPString, $iAD_SearchScope, "samaccountname,distinguishedname", "", False)
If @error Then
$iAD_Count = 0
Else
$iAD_Count = $aObjects[0][0]
EndIf
Else
$iAD_Count = _AD_GetObjectsInOU($aAD_TreeView[$iAD_Index][1], $sAD_LDAPString, $iAD_SearchScope, "samaccountname,distinguishedname", "", True)
EndIf
EndIf
$sAD_Temp = ""
If $bAD_Count And $sAD_LDAPString <> "" Then $sAD_Temp = StringReplace($sAD_Text, "%", $iAD_Count)
If $iAD_Level = 1 Then
$ahAD_Node[$iAD_Level] = _GUICtrlTreeView_Add($hAD_TreeView, 0, StringMid($sAD_Line[1], $iAD_Level + 1) & $sAD_Temp)
Else
$ahAD_Node[$iAD_Level] = _GUICtrlTreeView_AddChild($hAD_TreeView, $ahAD_Node[$iAD_Level - 1], StringMid($sAD_Line[1], $iAD_Level + 1) & $sAD_Temp)
EndIf
$aAD_Result[$iAD_OutIndex][0] = $aAD_TreeView[$iAD_Index][0]
$aAD_Result[$iAD_OutIndex][1] = $aAD_TreeView[$iAD_Index][1]
$aAD_Result[$iAD_OutIndex][2] = $ahAD_Node[$iAD_Level]
; Add the objects of the OU to the TreeView
If IsArray($aObjects) Then
ReDim $aAD_Result[UBound($aAD_Result, 1) + $aObjects[0][0]][UBound($aAD_Result, 2)]
For $iAD_Index2 = 1 To $aObjects[0][0]
$iAD_OutIndex += 1
If StringRight($aObjects[$iAD_Index2][0], 1) = "$" Then $aObjects[$iAD_Index2][0] = StringLeft($aObjects[$iAD_Index2][0], StringLen($aObjects[$iAD_Index2][0]) - 1)
$aAD_Result[$iAD_OutIndex][0] = $aObjects[$iAD_Index2][0]
$aAD_Result[$iAD_OutIndex][1] = $aObjects[$iAD_Index2][1]
$aAD_Result[$iAD_OutIndex][2] = _GUICtrlTreeView_AddChild($hAD_TreeView, $ahAD_Node[$iAD_Level], $aObjects[$iAD_Index2][0])
Next
Endif
Next
If $bAD_IsADOpen = False Then _AD_Close()
_GUICtrlTreeView_EndUpdate($hAD_TreeView)
$aAD_Result[0][0] = UBound($aAD_Result, 1) - 1
$aAD_Result[0][1] = UBound($aAD_Result, 2)
Return $aAD_Result

EndFunc   ;==>_AD_GetOUTreeView

C:Program Files (x86)AutoIt3IncludeAD.au3 (455) : ==> Variable must be of type "Object".:
$__oAD_Command.CommandText = "<LDAP://" & $sAD_HostServer & "/" & $sAD_DNSDomain & ">;(sAMAccountName=" & $sAD_SamAccountName & ");distinguishedName;subtree"
$__oAD_Command^ ERROR

Note:

UDF Version ...: 1.3.0.0

Autoit Version : 3.3.81.

do i need to update Autoit ?


Thank you,Regards,[font="Garamond"][size="4"]K.Syed Ibrahim.[/size][/font]

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