#205632 - 2012-08-31 09:35 PM
Re: how to list computers that are a member of an active directory group or OU
[Re: ShaneEP]
|
Robdutoit
Hey THIS is FUN
 
Registered: 2012-03-27
Posts: 347
Loc: London, England
|
Oh thanks Shane, I was just about to post to say that I have finally found the solution.
If you want to make an official UDF for enumerating computers/users etc in an OU using this coding, I think that would be fantastic. As you can see, I had found your typo by the time that I returned to the kixtart forum. I have also modified your script to remove your last line "get $" as I don't see the point in that line as the script works perfectly without the line.
Dim $objAdsPath, $obj, $filter[0] $filter[0] = "Computer" $objADsPath = GetObject("LDAP://OU=IT Suite,OU=Computers,OU=whatever,DC=domain,DC=internal") If @Error = 0 $objAdsPath.filter = $filter For Each $obj In $objAdsPath ? $obj.Name Next Else ? "Not able to connect to LDAP path." Endif
Give credits to yourself and to Arend for this script. I think that this should be in an official UDF because I think that this is an incredibly useful function, because in my search for this solution, I came across hundreds of posts asking pretty much the same question.
Before you do that, I have encountered one strange thing. If I run the script on an OU with computers, groups and users etc, it will return only the computers, but when I changed this line to read as this , the script runs and it returns the names of all users as expected, BUT it also returns the names of the computers ???? I cannot understand why it returns computers names if the filter is for users.
Edited by Robdutoit (2012-08-31 10:47 PM) Edit Reason: forgot to remove real domain name
|
Top
|
|
|
|
#205633 - 2012-08-31 09:40 PM
Re: how to list computers that are a member of an active directory group or OU
[Re: Robdutoit]
|
Robdutoit
Hey THIS is FUN
 
Registered: 2012-03-27
Posts: 347
Loc: London, England
|
To Mart thanks for your coding, but as you probably realised, I was not looking to find out the OU that the computer belongs to, but rather to find out what computers exist in an OU!
Allen, I tried your coding. I had to modify it as it came up with errors. Once I got rid of all the errors, the script seemed to enumerate all the Organistional Units rather than the computers within, so I will go with the script that Arend and Shanep put together as this script does the job beautifully.
In case anyone wonders why I was having problems with the path, its because I was doing the OU nesting backwards ie I should have put OUA in OUB in OUC, not OUC, OUB, OUA if that makes sense.
|
Top
|
|
|
|
#205637 - 2012-08-31 11:56 PM
Re: how to list computers that are a member of an active directory group or OU
[Re: ShaneEP]
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4537
Loc: USA
|
I cannot understand why it returns computers names if the filter is for users.
Ahhh, now its coming back to me why I stopped working on this, every time I turned around I was hunting for something else that didn't make sense. I think I know how to fix this though... All of this is in the function I referred you too ealier.
Dim $objAdsPath, $obj, $filter[0]
$filter[0] = "computer"
$objADsPath = GetObject("LDAP://OU=IT Suite,OU=Computers,OU=whatever,DC=domain,DC=internal")
If @Error = 0
$objAdsPath.filter = $filter
For Each $obj In $objAdsPath
if $filter[0]="User"
if $obj.class="user"
? $obj.name
endif
else
? $obj.name
endif
Next
Else
? "Not able to connect to LDAP path."
Endif
By the way, instead of using the quote tags, use the code tags instead, which is the next one beside the quote tag button.
|
Top
|
|
|
|
#205638 - 2012-09-01 12:10 AM
Re: how to list computers that are a member of an active directory group or OU
[Re: Allen]
|
ShaneEP
MM club member
   
Registered: 2002-11-29
Posts: 2120
Loc: Tulsa, OK
|
Here's a start to the function, if you want to test it out.
Function ObjectsInOU(OPTIONAL $OU, OPTIONAL $filter)
Dim $f[0],$objs[0],$x,$objADsPath,$obj
If not $OU
$OU = GetObject("LDAP://"+CreateObject("ADSystemInfo").ComputerName).Parent
If @Error
Exit @Error
Endif
Endif
$objADsPath = GetObject($OU)
If @Error = 0
If $filter
$f[0] = $filter
$objAdsPath.filter = $f
Endif
$x = 0
For Each $obj In $objAdsPath
If $filter="user"
If $obj.Class="User"
$objs[$x] = Split($obj.Name,"=")[1]
$x = 1+$x
ReDim Preserve $objs[$x]
Endif
Else
$objs[$x] = Split($obj.Name,"=")[1]
$x = 1+$x
ReDim Preserve $objs[$x]
Endif
Next
If UBound($objs) > 0
ReDim Preserve $objs[UBound($objs)-1]
Endif
$ObjectsInOU = $objs
Else
Exit @Error
Endif
EndFunction It would be called like so...$objects = ObjectsInOU("LDAP://OU=folder,...","Computer")
If @Error = 0
For each $o in $objects
? $o
Next
Else
? "Error: "+@Error
Endif
Edited by ShaneEP (2012-09-01 12:13 AM)
|
Top
|
|
|
|
#205641 - 2012-09-01 12:44 AM
Re: how to list computers that are a member of an active directory group or OU
[Re: Robdutoit]
|
Robdutoit
Hey THIS is FUN
 
Registered: 2012-03-27
Posts: 347
Loc: London, England
|
Hi Shanep, I will test out your UDF tomorrow morning as its now midnight here and I need my beauty sleep. My only question at this point is why its necessary to change the script so much for the udf. We didn't need redim and ubounds and why can't we use a simple
$Domain=@domain
GetObject("LDAP://" + $namesOfOus + "/" + $Domain)
instead of your
GetObject("LDAP://"+CreateObject("ADSystemInfo").ComputerName).Parent
where does this creatobject(ADSysteminfo) do?
|
Top
|
|
|
|
#205642 - 2012-09-01 12:49 AM
Re: how to list computers that are a member of an active directory group or OU
[Re: Robdutoit]
|
ShaneEP
MM club member
   
Registered: 2002-11-29
Posts: 2120
Loc: Tulsa, OK
|
Most of the complication in the function comes in storing all the results into an array that can be used in whatever way you want. The ReDims are necessary as the function never knows how many objects are going to be found...It essentially adds an element to the array for each object.
This line...GetObject("LDAP://"+CreateObject("ADSystemInfo").ComputerName).Parent simply pulls in the OU of the current computer, which is then used only if an OU is not specified when the function is called.
Edited by ShaneEP (2012-09-01 12:50 AM)
|
Top
|
|
|
|
#205645 - 2012-09-01 04:10 PM
Re: how to list computers that are a member of an active directory group or OU
[Re: ShaneEP]
|
Arend_
MM club member
   
Registered: 2005-01-17
Posts: 1892
Loc: Hilversum, The Netherlands
|
I've created a proper UDF out of my code, and added Allen's proper way of filtering.
|
Top
|
|
|
|
#205674 - 2012-09-06 04:50 PM
Re: how to list computers that are a member of an active directory group or OU
[Re: Robdutoit]
|
Allen
KiX Supporter
   
Registered: 2003-04-19
Posts: 4537
Loc: USA
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 116 anonymous users online.
|
|
|