Page 1 of 1 1
Topic Options
#85571 - 2002-05-02 05:15 PM Enumerate all users in AD
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
I am familiar with $user = getobject("WinNT://$domain/$userid,user") in order to query a single user object, but I cannot find how to enumerate all user objects in the active dir???

I checked the SDK and MSDN but found nothing...

Frustrating...

-Jim

*********************
Nevermind... Found it!

code:
  
break on
$domain = @ldomain
$AD = getobject("WinNT://$domain")

for each $userobj in $AD
if $userobj.class="User"

? $userobj.name
EndIf
Next
$AD=""



[ 02 May 2002, 17:28: Message edited by: jtokach ]
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#85572 - 2002-05-02 05:30 PM Re: Enumerate all users in AD
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
You can also use the filter.
code:
$DomObj = getobject("WinNT://dss_test,domain")
$DomObj.filter = "user",""
for each $user in $DomObj
? $user.name
next

_________________________
Home page: http://www.kixhelp.com/hb/

Top
#85573 - 2002-05-02 06:31 PM Re: Enumerate all users in AD
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Thanks! Ya know what though, I expected to see an increase in time with your version, but it seems the same? Anyhow, I now have bigger problems...

We have +30,000 users on our domain. The list above takes 30 seconds to display. I need only to find users who belong to groups with the prefix abc. (i.e. abc.accounting, abc.purchasing)

This works but would probably take 30-45 minutes to develop a list. Any suggestions on improving performance?

code:
  
Break On
$DomObj = getobject("WinNT://@ldomain,domain")
$DomObj.filter = "user",""
For Each $User in $DomObj
For Each $Group In $User.Groups
If Left($Group.name,3)="abc"
Goto Pass
EndIf
Next
Goto Fail
:Pass
? $user.name
:Fail
Next

I'm not familiar with the possiblities of the filter method, this is most likely a good starting point though... [Smile]
-Jim
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#85574 - 2002-05-02 06:53 PM Re: Enumerate all users in AD
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
have a go with this.

code:
$domain = getobject("WinNT://@ldomain")

$domain.filter = "group",""

for each $group in $domain
if substr($group.name,1,3) = "abc"
? $group.name + ".."
for each $user in $group.members
? $user.name
next
endif
next

if these are global groups and if you have local groups in the global groups, then the local groups will be returned in the $user object as well.

Use $user.class to filter out only user accounts.

Bryce

[ 02 May 2002, 18:57: Message edited by: Bryce ]

Top
#85575 - 2002-05-02 07:08 PM Re: Enumerate all users in AD
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
That's pretty much what I was going to say...

Been searching the help file for half an hour to work out the syntax... meanwhile, Bryce can knock this off in his sleep. [Big Grin]
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#85576 - 2002-05-02 07:24 PM Re: Enumerate all users in AD
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Bryce,
Much better thought process! Thanks a bunch!

Do you happen to have(know of) a routine to remove duplicates? IsInArray() will be subject to the above limitations... read/compare, read/compare, read/compare... Is there no end to the madness?

Thanks again!

Les,
It makes you sick, doesn't it? [Big Grin]
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#85577 - 2002-05-02 07:49 PM Re: Enumerate all users in AD
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
here ya go.

this should give you an array called $userlist, this array will be a list of all the users who are in your abc* groups with no duplicates.

code:
break on

$domain = getobject("WinNT://@ldomain")

Dim $userlist[5]
$i = 0

$domain.filter = "group",""

for each $group in $domain
if substr($group.name,1,3) = "abc"
for each $user in $group.members
if $user.class = "user"
for each $name in $userlist
if $name = $user.name
$flag = 1
endif
next
if $flag <> 1
$userlist[$i] = $user.name
$i = $i + 1
if ubound($userlist) = $i-1
redim preserve $userlist[ubound($userlist) + 5]
endif
else
$flag = 0
endif
endif
next
endif
next

redim preserve $userlist[$i-1]

for each $name in $userlist
? $name
next


Top
#85578 - 2002-05-02 09:38 PM Re: Enumerate all users in AD
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
My head is spinning! [Big Grin] I just sat here for the past 15 minutes pressing enter (debugging your code) to figure it out. So simple, yet BRILLIANT! You veterans never cease to amaze me! Sure beats the hell out of comparing every item in the array!

Final time on 422 (of 4592) groups, containing +3700 users (of 30000), Captured 574 users in 1Min 28secs. Not bad!

(IsInArray took twice as long)

Thanks!
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#85579 - 2002-05-03 12:00 AM Re: Enumerate all users in AD
kholm Offline
Korg Regular
*****

Registered: 2000-06-19
Posts: 714
Loc: Randers, Denmark
Why not break as soon as the user is found in the list ?

I have taken the liberty to make some small changes in Bryce's script [Eek!]

This should perform even better:
code:
break on

$domain = getobject("WinNT://@ldomain")

Dim $userlist[5]
$i = 0

$domain.filter = "group",""

for each $group in $domain
if substr($group.name,1,3) = "abc"
for each $user in $group.members
if $user.class = "user"
$j = 0
$NotInList = 1
While $j <= $i And $NotInList
if $userlist[$j] = $user.name
$NotInList = 0 ; User found, stop searching
endif
Loop
if $NotInList
$userlist[$i] = $user.name
$i = $i + 1
if ubound($userlist) = $i-1
redim preserve $userlist[ubound($userlist) + 5]
endif
endif
endif
next
endif
next

redim preserve $userlist[$i-1]

for each $name in $userlist
? $name
next

-Erik

Top
#85580 - 2002-06-24 11:25 AM Re: Enumerate all users in AD
Mark Bennett Offline
Getting the hang of it

Registered: 2001-10-10
Posts: 70
I want to filter by a specific AD container. I have had a quick play around but can't work out the filter syntax. Can anyone help?

Thanks,
Mark

Top
#85581 - 2002-06-25 01:04 PM Re: Enumerate all users in AD
Mark Bennett Offline
Getting the hang of it

Registered: 2001-10-10
Posts: 70
Sorted - If I had spent some more time researching (as I did yesterday afternoon), I would have found the code below sooner! Have included the code in case someone else goes looking for it.

code:
  

$domain = GetObject("WinNT://@LDOMAIN")
$domain.filter = "group",""
For Each $group in $domain
If SubStr($group.name,1,31) = "Software - Office Tools"
? $group.name + ".."
For Each $user in $group.members
$1 = $user.name
Shell '%COMSPEC% /C net group "Software - Office Tools" $1 /delete /domain'
? " $1 " Color g+/n "Updated"
Next
EndIf
Next


Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1376 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.073 seconds in which 0.029 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org