Sign in to follow this  
Followers 0
kor

Help to query list of computers in an OU (adfunctions)

6 posts in this topic

I'm trying

#include <adfunctions.au3>

Global $ObjectArray, $ou="Computers", $filter, $searchscope, $datatoretrieve, $sortby

_ADGetObjectsInOU($ObjectArray, $ou, $filter, $searchscope, $datatoretrieve, $sortby)
_ArrayDisplay($ObjectArray)

But I get a COM error.

I'm still a little new on how to call functions from Includes and also provide all the needed VAR's.

Basically, I need to query a certain OU for the list of computer objects that are already there.

Then I will need to delete all the computer objects.

Finally, re-add all the objects that were already there, and set the permission on who can join the computer to the domain to a specific group.

(for pre-staging)

Share this post


Link to post
Share on other sites



I have been able to get the first part of the script. This will query an OU and return a list of computers in that OU.

#include <adfunctions.au3>
#include <Array.au3>

Global $Computers
$ou = "OU=Workstations,OU=NOC,DC=ad,DC=local,DC=com"; Root of your AD, e.g. DC=microsoft,DC=com
_ADGetObjectsInOU($Computers, $ou, "(&(objectCategory=computer))", 2)
_ArraySort($Computers, 0, 1)
_ArrayDisplay($Computers)

Share this post


Link to post
Share on other sites

I'm having some trouble with my second part of deleteing all the computers in an OU.

Dim $DelComputers
_FileReadToArray("C:\Users\test\Desktop\image\output.txt", $DelComputers)
_ArrayDelete($DelComputers, 0);removes the first row of array to get rid of count

Dim $Average, $Array2Size = UBound($DelComputers)
For $i = 0 To $Array2Size
    _ADDeleteObject($ou, $DelComputers[$i], "computer")
Next

This code works, but it's only deleting the first computer in the array instead of all of them. Can someone help me with my loop?

Share this post


Link to post
Share on other sites

Nevermind, got it.

Dim $Array2Size = UBound($DelComputers)-1
For $i = 0 To $Array2Size
    _ADDeleteObject($ou, $DelComputers[$i], "computer")
Next

Share this post


Link to post
Share on other sites

This works nicely.

#include <adfunctions.au3>
#include <Array.au3>
#include <File.au3>

;---------------
; Read OU
;---------------
Global $Computers
$ou = "OU=test,DC=ad,DC=local,DC=com"; Root of your AD, e.g. DC=microsoft,DC=com
_ADGetObjectsInOU($Computers, $ou, "(&(objectCategory=computer))", 2)

Dim $Array1Size = UBound($Computers)-1
;~ _ArrayDisplay($Sizes)
For $i = 0 To $Array1Size
    $Computers[$i] = StringReplace($Computers[$i], '$', '');removes money sign from end of each computer
    $Computers[$i] = StringLower($Computers[$i]);makes all computers lowercase
Next

If $Computers = "" Then
    MsgBox(64, "No objects", "There are no objects in this OU")
    Exit
EndIf

_ArraySort($Computers, 0, 1);sort alphabetically a-z
_ArrayDelete($Computers, 0);removes the first row of array to get rid of count
;_ArrayDisplay($Computers)
_FileWriteFromArray("C:\Users\test\Desktop\image\output.txt", $Computers);output OU to text file


;--------------------------
; Delete Computers from OU
;--------------------------
Dim $DelComputers
_FileReadToArray("C:\Users\test\Desktop\image\output.txt", $DelComputers)
_ArrayDelete($DelComputers, 0);removes the first row of array to get rid of count

Dim $Array2Size = UBound($DelComputers)-1
For $i = 0 To $Array2Size
    _ADDeleteObject($ou, $DelComputers[$i], "computer")
Next


;--------------------------
; Create Computers in OU
;--------------------------
Dim $CreateComputers
_FileReadToArray("C:\Users\test\Desktop\image\output.txt", $CreateComputers)
_ArrayDelete($CreateComputers, 0);removes the first row of array to get rid of count
$computerOU = $ou

Dim $Array3Size = UBound($CreateComputers)-1
For $i = 0 To $Array3Size
    _ADCreateComputer($CreateComputers[$i], $computerOU, "Desktop Support")
Next

Share this post


Link to post
Share on other sites

Hi all,

is that possible to get lastlogon attribute of each computer in same time with this function ?

Bye

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