Page 1 of 1 1
Topic Options
#193366 - 2009-04-06 12:24 PM Getting all users from specific OU
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Guys,

Small question.
We have a project that requires us to get the full name for each user in a specific OU. The OU is variable and we need to specify 7 (for now) separate OU’s one by one or in an array that is to be determined later on. But that is not the problem

I got the script below that works for me but it is VBS. Tried to convert it to kix but no success, maybe because of my very very low VBS knowledge

Can this be converted to kix in some way or is it just impossible?

 Code:
On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

objCommand.CommandText = _
"SELECT Name FROM 'LDAP://ou=some_ou,dc=subdomain,dc=domain,dc=tld' WHERE objectCategory='user'"  
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
	WScript.Echo objRecordSet.Fields("Name").Value
	objRecordSet.MoveNext
Loop
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193367 - 2009-04-06 01:48 PM Re: Getting all users from specific OU [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Did an almost literal translation several times but no working code yet.

Especially the "objCommand.Properties.Searchscope" line is causing some issues. Error -2147352570 - Unknown name that translates to Error 6 - The handle is invalid using the fnCOMErr() UDF.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193368 - 2009-04-06 02:48 PM Re: Getting all users from specific OU [Re: Mart]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4557
Loc: USA
Will Translatename() work?
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=83002#Post83002

Top
#193369 - 2009-04-06 03:03 PM Re: Getting all users from specific OU [Re: Allen]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Not in this case.
All I want to do is give the script an OU (like ou=someou,dc=subdomain,dc=domain,dc=ext) and have it spit out all full names for all users in that OU and all sub OU's into an array.

The script above works great but it is in VBS and I would like to have it in kix if possible.

I know fnLDAPQuery can do this but I do not see how to make it accept a OU specified by me.

LDAP scripting if kinda new to me and I get stuck very fast.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193370 - 2009-04-06 03:18 PM Re: Getting all users from specific OU [Re: Mart]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4557
Loc: USA
I think you should definitely search in the UDFs as I can't believe what you are looking for hasn't already been done.

In the mean time, I remember creating a UDF years ago that would do something like what you are needing. I have not used it in at least 5 years and when I wrote it, it was before I really felt comfortable with kix or com... but it might still be valuable: http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Main=12245&Number=75055#Post75055

Also... would mind posting your conversion of the vbs.

Top
#193371 - 2009-04-06 03:33 PM Re: Getting all users from specific OU [Re: Allen]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Jeez, that's a golden oldie Allen. Six years. I do some tests and keep you informed.

My literal translation of the VBS script (does not work):
 Code:
$objConnection = CreateObject("ADODB.Connection")
$objCommand = CreateObject("ADODB.Command")
$objConnection.Provider = "ADsDSOObject"
$objConnection.Open "Active Directory Provider"
$objCommand.ActiveConnection = $objConnection

$objCommand.Properties("Page Size") = 1000
$objCommand.Properties("Searchscope") = 2

$objCommand.CommandText = "SELECT Name FROM 'LDAP://ou=some_ou,dc=subdomain,dc=domain,dc=tld' WHERE objectCategory='user'"  
$objRecordSet = $objCommand.Execute

$objRecordSet.MoveFirst
Do Until $objRecordSet.EOF
	? $objRecordSet.Fields("Name").Value
	$objRecordSet.MoveNext
Loop
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193372 - 2009-04-06 03:34 PM Re: Getting all users from specific OU [Re: Allen]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Hey Mart,

this code is old, but maybe it will get you started:
 Code:
$objRootDSE = GetObject("LDAP://rootDSE")
$defaultNamingContext = $objRootDSE.Get("defaultNamingContext")

GetOU("staff",$defaultNamingContext)

? "Done!"

Get $a

Function GetOU($OrgUnit,$defaultNamingContext)

	;$Domain.organizationalUnit
  $Domain = GetObject("LDAP://" + $defaultNamingContext)

	For Each $OU1 in $Domain
		If $OU1.Class = "organizationalUnit"
			$Object = $OU1.Name
      $OUArray = Split($Object,",")
			If $OUArray[0] = "OU=" + $OrgUnit
				? $Object
				$Users = GetObject("LDAP://" + $Object + "," + $defaultNamingContext)
				For Each $User in $Users
					If $User.Class = "User"
						? "     " + $User.Name
					EndIf
				Next
			EndIf
			GetOU1($Object,$OrgUnit,$defaultNamingContext)
		EndIf
	Next

EndFunction

Function GetOU1($Object,$OrgUnit,$defaultNamingContext)
	
	$Domain1 = GetObject("LDAP://" + $Object + "," + $defaultNamingContext)
	
	;$Domain.organizationalUnit
	For Each $OU2 in $Domain1
		If $OU2.Class = "organizationalUnit"
			$Object1 = $OU2.Name
			$Object2 = $Object1 + "," + $Object
      $OUArray = Split($Object2,",")
			If $OUArray[0] = "OU=" + $OrgUnit
				? $Object2
				$Users = GetObject("LDAP://" + $Object2 + "," + $defaultNamingContext)
				For Each $User in $Users
					If $User.Class = "User"
						? "     " + $User.Name
					EndIf
				Next
			EndIf
			GetOU1($Object2,$OrgUnit,$defaultNamingContext)
		EndIf
	Next

EndFunction
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#193373 - 2009-04-06 04:19 PM Re: Getting all users from specific OU [Re: Benny69]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Benny, Allen,

Thanks for the suggestions.

Allen,

Your code only gives me errors. Dunno why but I cannot get it to work.

Benny,

Works sort of. I get al users but I cannot figure our how to modify it to have it accept an OU and get all users from it and it's sub OU's.



I got something going with the InContainer() and TranslateName() UDF's
Now I just need to get it going with a array of all users (I can get all users easily), let it step trough the array(also easy) and do it's magic (not that easy to have InConatiner accept a username. At least I do not know how). The challenges just keep on coming. Educational yes but also frustrating Google is my friend today and maybe Howard will stop by with a suggestion on how to change his UDF.

 Code:
Break on

$countries = "Belgium", "Corporate", "France", "Germany", "Italy", "Spain", "Switzerland", "The Netherlands"

For Each $country in $countries
	$rc = InContainer("OU=" + $country + ",OU=Company_OU,DC=Domain,DC=Domain,DC=Ext", "User")
	If $rc[0] = "1" Or $rc[0] = "2"
		? "Yep this user is a member of the exact OU or a sub OU " $country
	Else
		? "Nope user is NOT a member of the exact OU Or a sub OU " $country
	EndIf
Next

Sleep 3
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193374 - 2009-04-06 04:32 PM Re: Getting all users from specific OU [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
LOL

Easier done then said.

Changed one line in the UDF and added a third optional parameter ($userid) and all is working just great. May need some shaving and polishing but the basics are there. Sweet
The only thing that remains is how to get the users full name after checking that he/she is a member of the correct OU.

 Code:
Break on

$countries = "Belgium", "Corporate", "France", "Germany", "Italy", "Spain", "Switzerland", "The Netherlands"

For Each $country in $countries
	$rc = InContainer("OU=" + $country + ",OU=Company_OU,DC=Domain,DC=Domain,DC=Ext", "User", "username_goes_here")
	If $rc[0] = "1" Or $rc[0] = "2"
		? "Yep this user is a member of the exact OU or a sub OU " $country
	Else
		? "Nope user is NOT a member of the exact OU Or a sub OU " $country
	EndIf
Next

Sleep 5

;FUNCTION         InContainer()
;
;AUTHOR           Howard A. Bullock (hbullock@tycoelectronics.com)
;
;VERSION          1.5
;
;DATE             20-Mar-2002
;REVISED          04-Apr-2005
;
;ACTION           Determines if the current NT4 account name type is a member of a specific container (OU, Computers, etc)
;                 in Active Directory
;
;SYNTAX           InContainer ($Container, $NameType)
;
;PARAMETERS       $Container (Required)
;                  -  String value
;                 Dinstinghished name of the container to check. This must be the fully qualified DN to
;                 accurately make a determination.
;
;                 $NameType (Required)
;                  -  String value
;                 "Computer" or "User" are currently the only valid values
;
;REMARKS          This function returns true if the object being checked in the the specified container
;                 or a child container of that specified.
;
;RETURNS          An ARRAY of three values:
;                    InContainer return code
;                      1 = object is a member of the exact container specified.
;                      2 = object is a member of the container hierarchy.
;                      0 = object is not a member of the container hierarchy.
;                     -1 = Invalid input for $NameType
;                     -2 = Error in TranslateName
;                    TranslateName ErrorCode
;                    TranslateName ErrorText
;
;DEPENDENCIES     OS: Active Directory aware client
;                 Other Functions: TranslateName()
;
;EXAMPLES         $rc = InContainer ("OU=test,OU=9826,OU=NCS,OU=Machines,DC=us,DC=tycoelectronics,DC=com", "Computer")
;                 select
;                   case $rc[0]=1  ? "object is a member of the specified container."
;                   case $rc[0]=2  ? "object is a member of a child container lower in the hierarchy."
;                   case $rc[0]=0  ? "object is NOT a member of this container or a child of this container."
;                   case $rc[0]=-1 ? "InContainer() Error - Invalid input for $NameType "
;                   case $rc[0]=-2 ? "TranslateName() Error"
;                   case 1         ? "Unknown return code"
;                 endselect
;
;
Function InContainer($Container, $NameType, $userid)
	Dim $CurrentContainer, $Name1, $Name2, $Found, $commaloc
	
	Select
		Case $NameType = "Computer" $Name1 = @Domain + "\" + @wksta + "$$"
		Case $NameType = "User" $Name1 = @LDomain + "\" + $userid
		;Case $NameType = "User" $Name1 = @LDomain + "\" + @UserID
		Case 1 $Name1 = ""
	EndSelect
	
	If $Name1 <> ""
		$Name2 = TranslateName(3, "", 3, $Name1, 1)
		
		If $Name2[1] = 0
			
			$Found = 0
			While $Found = 0
				$commaloc = InStr($Name2[0], ",")
				If $commaloc > 1
					If SubStr($Name2[0], $commaloc - 1, 1) = "\"
						$Name2[0] = SubStr($Name2[0], $commaloc + 1)
					Else
						$Found = 1
						$CurrentContainer = SubStr($Name2[0], $commaloc + 1)
					EndIf
				Else
					$Found = 1
				EndIf
			Loop
			
			Select
				Case $CurrentContainer = $Container $InContainer = 1, $Name2[1], $Name2[2]
				Case InStr($Name2[0], $Container) $InContainer = 2, $Name2[1], $Name2[2]
				Case 1 $InContainer = 0, $Name2[1], $Name2[2]
			EndSelect
		Else
			$InContainer = -2, $Name2[1], $Name2[2]
		EndIf
	Else
		$InContainer = -1, 0, ""
	EndIf
EndFunction

;FUNCTION         TranslateName()
;
;AUTHOR           Howard A. Bullock (hbullock@tycoelectronics.com)
;
;VERSION          2.0
;
;ACTION           Translates from one name type to another. Good for converting an NT4 name
;                 like domain\user into an LDAP distinguished name or the reverse.
;
;SYNTAX           TranslateName ($InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType)
;
;PARAMETERS       $InitType (Required)
;                  -  Integer value
;                     1 = ADS_NAME_INITTYPE_DOMAIN
;                     Initializes a NameTranslate object by setting the domain that the object will bind to.
;
;                     2 = ADS_NAME_INITTYPE_SERVER
;                     Initializes a NameTranslate object by setting the server that the object will bind to.
;
;                     3 = ADS_NAME_INITTYPE_GC
;                     Initializes a NameTranslate object by locating the global catalog to which the object
;                     will bind.
;
;                 $BindName (Required)
;                  -  String value
;                     If an $InitType = 3 (ADS_NAME_INITTYPE_GC), then the $BindName = "".
;                     InitTypes 1 and 2 require a name of a domain or server to be input. 
;                     Note: "" may default to the current server or domain.
;
;                 $LookupNameType (Required)
;                  -  Integer value
;
;                 $LookupName (Required)
;                  -  String value see below
;
;                 $ReturnNameType (Required)
;                  -  Integer value see below
;
;                  Documentation of Name Types. Lookup the more info on http://MSDN.Microsoft.com
;                  Not all name types work. "1", "2", and "3" have been the most useful. 
;
;                 1 = ADS_NAME_TYPE_1779
;                 Name format as specified in RFC 1779. For example, "CN=Jane Doe,CN=users, DC=Microsoft, DC=com".
;
;                 2 = ADS_NAME_TYPE_CANONICAL
;                    Canonical name format. For example, "Microsoft.com/Users/Jane Doe".
;
;                 3 = ADS_NAME_TYPE_NT4
;                 Account name format used in Microsoft® Windows© NT® 4.0. For example, "Microsoft\JaneDoe".
;
;                 4 = ADS_NAME_TYPE_DISPLAY
;                 Display name format. For example, "Jane Doe".
;
;                 5 = ADS_NAME_TYPE_DOMAIN_SIMPLE
;                 Simple domain name format. For example, "JaneDoe@Microsoft.com".
;
;                 6 = ADS_NAME_TYPE_ENTERPRISE_SIMPLE
;                 Simple enterprise name format. For example, "JaneDoe@Microsoft.com".
;
;                 7 = ADS_NAME_TYPE_GUID
;                 Global Unique Identifier format. For example, {95ee9fff-3436-11d1-b2b0-d15ae3ac8436}.
;
;                 8 = ADS_NAME_TYPE_UNKNOWN
;                 Unknown name type. The system will try to make the best guess.
;
;                 9 = ADS_NAME_TYPE_USER_PRINCIPAL_NAME
;                 User principal name format. For example, "JaneDoe@Fabrikam.com".
;
;                 10 = ADS_NAME_TYPE_CANONICAL_EX
;                 Extended canonical name format. For example, "Microsoft.com/Users Jane Doe".
;
;                 11 = ADS_NAME_TYPE_SERVICE_PRINCIPAL_NAME
;                 Service principal name format. For example, "www/www.microsoft.com@microsoft.com"
;
;                 12 = ADS_NAME_TYPE_SID_OR_SID_HISTORY_NAME
;                 A SID string, as defined in the Security Descriptor Definition Language (SDDL), for either
;                 the SID of the current object or one from the object's SID history.
;                 For example, "O:AOG:DAD:(A;;RPWPCCDCLCSWRCWDWOGA;;;S-1-0-0)" For more information see
;                 Security Descriptor String Format under "Security" in the Microsoft Platform SDK documentation.
;
;REMARKS          Not name types seem to work.
;
;RETURNS          This function returns an ARRAY of three values:
;                               Name of the type specified by $ReturnNameType (String)
;                               Error number (Long Integer)
;                               Error text (String).
;
;DEPENDENCIES     OS: Active Directory aware client
;
;EXAMPLES         $DN = TranslateName (3, "", 3, "@Domain\@wksta$", 1)
;                 ? "DN = " + $DN[0]
;                 ? "Error = " + $DN[1]
;                 ? "ErrorText = " + $DN[2]
;
;                 $DN = TranslateName (3, "", 3, "@LDomain\@userid", 1)
;                 ? "DN = " + $DN[0]
;                 ? "Error = " + $DN[1]
;                 ? "ErrorText = " + $DN[2]
;
;
Function TranslateName($InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType)
	
	Dim $InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType
	Dim $NameTranslate, $ReturnName, $Error, $ErrorText
	
	$Error = 0
	$ErrorText = ""
	$ReturnName = ""
	$NameTranslate = CreateObject("NameTranslate")
	$Error = @error
	$ErrorText = @serror
	If $Error = 0
		$NameTranslate.Init($InitType, $BindName)
		$Error = @error
		$ErrorText = @serror
		If $Error = 0
			$NameTranslate.Set($LookupNameType, $LookupName)
			$Error = @error
			$ErrorText = @serror
			If $Error = 0
				$ReturnName = $NameTranslate.Get($ReturnNameType)
				$Error = @error
				$ErrorText = @serror
			EndIf
		EndIf
	EndIf
	$TranslateName = $ReturnName, $Error, $ErrorText
EndFunction
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193375 - 2009-04-06 04:39 PM Re: Getting all users from specific OU [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Even more success. Today is a good day.

All is working now. I'll post some cleaned up code shortly.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193376 - 2009-04-06 05:19 PM Re: Getting all users from specific OU [Re: Mart]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
A bit late to the party, but this code snippet works for me:
 Code:
Break ON
$gNull=SetOption("ASCII","ON")
$gNull=SetOption("WrapAtEOL","ON")
$gNull=SetOption("Explicit","ON")

; GETU.KIX
; -----------------
;
; Iterate all user objects in an OU

Dim $sUser,$sOU
Dim $objConnection,$objCommand,$sSearchBase,$objRecordSet

$objConnection = CreateObject("ADODB.Connection")
$objCommand =   CreateObject("ADODB.Command")
$objConnection.Provider = "ADsDSOObject"
$objConnection.Open("Active Directory Provider")
$objCommand.ActiveConnection = $objConnection

$sSearchBase="ou=some_ou,dc=subdomain,dc=domain,dc=tld"

$objCommand.CommandText = 
   "SELECT Name,distinguishedName FROM " 
   + "'LDAP://"+$sSearchBase
   + "'"
   + " WHERE objectCategory='user'"
 
$objCommand.Properties("Page Size").Value = 100
$objCommand.Properties("Search Scope").Value = 2
$objCommand.Properties("Cache Results").Value = (not 1)

$objRecordSet = $objCommand.Execute()
$objRecordSet.MoveFirst
while not $objRecordSet.EOF
	$sUser=$objRecordSet.Fields("Name").Value
	$sOU=$objRecordSet.Fields("distinguishedName").Value
	Left($sUser+"                               ",30)+$sOU+@CRLF
	$objRecordSet.MoveNext
Loop

; vim600:ai ts=4 sw=4

Top
#193377 - 2009-04-06 05:35 PM Re: Getting all users from specific OU [Re: Mart]
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
here is the same code but gets all sub OUs and users:

 Code:
$objRootDSE = GetObject("LDAP://rootDSE")
$defaultNamingContext = $objRootDSE.Get("defaultNamingContext")

GetOU("staff",$defaultNamingContext)

? "Done!"

Get $a

Function GetOU($OrgUnit,$defaultNamingContext)

	;$Domain.organizationalUnit
  $Domain = GetObject("LDAP://" + $defaultNamingContext)

	For Each $OU1 in $Domain
		If $OU1.Class = "organizationalUnit"
			$Object = $OU1.Name
			If InStr($Object,$OrgUnit)
				? $Object
				$Users = GetObject("LDAP://" + $Object + "," + $defaultNamingContext)
				For Each $User in $Users
					If $User.Class = "User"
						? "     " + $User.Name
					EndIf
				Next
			EndIf
			GetOU1($Object,$OrgUnit,$defaultNamingContext)
		EndIf
	Next

EndFunction

Function GetOU1($Object,$OrgUnit,$defaultNamingContext)
	
	$Domain1 = GetObject("LDAP://" + $Object + "," + $defaultNamingContext)
	
	;$Domain.organizationalUnit
	For Each $OU2 in $Domain1
		If $OU2.Class = "organizationalUnit"
			$Object1 = $OU2.Name
			$Object2 = $Object1 + "," + $Object
			If InStr($Object,$OrgUnit)
				? $Object2
				$Users = GetObject("LDAP://" + $Object2 + "," + $defaultNamingContext)
				For Each $User in $Users
					If $User.Class = "User"
						? "     " + $User.Name
					EndIf
				Next
			EndIf
			GetOU1($Object2,$OrgUnit,$defaultNamingContext)
		EndIf
	Next

EndFunction
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#193378 - 2009-04-06 07:57 PM Re: Getting all users from specific OU [Re: Benny69]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Thanks Richard and Benny. I'll have a look at your solutions tomorrow morning (it's almost 8pm here) where I'm back at the office.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193394 - 2009-04-08 11:59 AM Re: Getting all users from specific OU [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Here's what I ended up with. This is code I feel comfortable with and I exactly understand what is does. I'm not a COM guru so that’s quite an achievement \:\)

The code requires the TranslateName() and a modified version of the InContainer() UDF. See the second code section in this post for the modiefied InContainer() UDF.

 Code:
Break on

$countries = "Belgium", "Corporate", "France", "Germany", "Italy", "Spain", "Switzerland", "The Netherlands"

;Get all users from the current domain.
$oDomain = GetObject("WinNT://@LDomain")
$oDomain.filter = "User", ""

For Each $oUser in $oDomain
	If $oUser.AccountDisabled = "0"
		$namearray = Split($oUser.Fullname, ",")
		If UBound($namearray) > 0
			For $i = 0 to UBound($namearray)
				$namearray[$i] = Trim( $namearray[$i])
			Next
			$fullname = $namearray[1] + " " + $namearray[0]
			For Each $country in $countries
				$rc = InContainer("OU=" + $country + ",OU=companyname_ou,DC=SubDomain,DC=Domain,DC=Ext", "User", $oUser.name)
				If $rc[0] = "1" Or $rc[0] = "2"
					MD "\\Server\Share\" + $fullname
				EndIf
			Next
		EndIf
	EndIf
Next


Modified InContainer() UDF:
 Code:
;FUNCTION InContainer()
;
;AUTHOR
;			Howard A. Bullock (hbullock@tycoelectronics.com)
;
;VERSION
;			1.6
;
;DATE
;			20-Mar-2002
;REVISED
;			04-Apr-2005
;			08-Apr-2009 - Mart - Added userid parameter.
;
;ACTION
;			Determines if the current NT4 account name type is a member of a specific container (OU, Computers, etc)
;			in Active Directory
;
;SYNTAX
;			InContainer ($Container, $NameType, $userid)
;
;PARAMETERS
;			$Container (Required)
;				-  String value
;				Dinstinghished name of the container to check. This must be the fully qualified DN to
;				accurately make a determination.
;
;			$NameType (Required)
;				-  String value
;				"Computer" or "User" are currently the only valid values
;
;			$Userid (Optional)
;				- The username to check.
;				If none is specified @userid is assumed.
;
;REMARKS
;			This function returns true if the object being checked in the the specified container
;			or a child container of that specified.
;
;RETURNS
;			An ARRAY of three values:
;			InContainer return code
;				1 = object is a member of the exact container specified.
;				2 = object is a member of the container hierarchy.
;				0 = object is not a member of the container hierarchy.
;				-1 = Invalid input for $NameType
;				-2 = Error in TranslateName
;				TranslateName ErrorCode
;				TranslateName ErrorText
;
;DEPENDENCIES
;			OS: Active Directory aware client
;			Other Functions: TranslateName()
;
;EXAMPLES
;			$rc = InContainer ("OU=test,OU=9826,OU=NCS,OU=Machines,DC=us,DC=tycoelectronics,DC=com", "Computer")
;			select
;				case $rc[0]=1
;					? "object is a member of the specified container."
;				case $rc[0]=2
;					? "object is a member of a child container lower in the hierarchy."
;				case $rc[0]=0
;					? "object is NOT a member of this container or a child of this container."
;				case $rc[0]=-1
;					? "InContainer() Error - Invalid input for $NameType "
;				case $rc[0]=-2
;					? "TranslateName() Error"
;				case 1
					;? "Unknown return code"
;			endselect
;
Function InContainer($Container, $NameType, optional $userid)
	Dim $CurrentContainer, $Name1, $Name2, $Found, $commaloc
	
	If Trim($userid) = ""
		$userid = @USERID
	EndIf
	
	Select
		Case $NameType = "Computer" $Name1 = @Domain + "\" + @wksta + "$$"
		Case $NameType = "User" $Name1 = @LDomain + "\" + $userid
		;Case $NameType = "User" $Name1 = @LDomain + "\" + @UserID
		Case 1 $Name1 = ""
	EndSelect
	
	If $Name1 <> ""
		$Name2 = TranslateName(3, "", 3, $Name1, 1)
		
		If $Name2[1] = 0
			
			$Found = 0
			While $Found = 0
				$commaloc = InStr($Name2[0], ",")
				If $commaloc > 1
					If SubStr($Name2[0], $commaloc - 1, 1) = "\"
						$Name2[0] = SubStr($Name2[0], $commaloc + 1)
					Else
						$Found = 1
						$CurrentContainer = SubStr($Name2[0], $commaloc + 1)
					EndIf
				Else
					$Found = 1
				EndIf
			Loop
			
			Select
				Case $CurrentContainer = $Container $InContainer = 1, $Name2[1], $Name2[2]
				Case InStr($Name2[0], $Container) $InContainer = 2, $Name2[1], $Name2[2]
				Case 1 $InContainer = 0, $Name2[1], $Name2[2]
			EndSelect
		Else
			$InContainer = -2, $Name2[1], $Name2[2]
		EndIf
	Else
		$InContainer = -1, 0, ""
	EndIf
EndFunction


Edited by Mart (2009-04-08 12:00 PM)
Edit Reason: Typo.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193395 - 2009-04-09 10:18 AM Re: Getting all users from specific OU [Re: Mart]
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 309
Loc: STRASBOURG, France
Hi,

you can do what you want with fnLDAPQuery function :
 Code:
break on

". searching user" ?
$what = "samAccountName", "displayname"
$from = "LDAP://OU=xxxx,OU=xxxx,OU=xxxx,DC=xx,DC=xx"
$sFilter = "(&(objectCategory=person)(objectClass=user))"

$arr = fnLDAPQuery( $What, $from, $sFilter, "samAccountName", "onelevel" )
if @error
	"  error in the LDAP request" ?
else
	"  found : " (UBound($arr)+1) ?

	". results" ?
	for $i = 0 to UBound($arr)
		for $j = 0 to UBound($arr,2)
			$arr[$i,$j] ";"
		next
		?
	next
endif

"== ended ==" ?
gets $x
exit 0
 
Function fnLDAPQuery($What,Optional $From, Optional $sFilter, Optional $OrderBy, Optional $Scope, Optional $User, Optional $Pswd)
...
...
endfunction 

There are two important points :
- $from is the distinguishedname of the OU prefixed with LDAP://
- $scope is set to "onelevel" to get object contained in the OU

if you want to get user in "Users", take care that Users is not an OU.
distinguished name is CN=Users,DC=xxx,DC=xx
so $from should be "LDAP://CN=Users,DC=cus,DC=fr"
_________________________
Christophe

Top
#193396 - 2009-04-09 11:21 AM Re: Getting all users from specific OU [Re: ChristopheM]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Thanks.
I'll run some tests today.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193458 - 2009-04-16 09:28 AM Re: Getting all users from specific OU [Re: Mart]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
Haven't really read the whole thread by the simplest way to list users in a specific ou would be this:
 Code:
$=SetOption('Explicit','On')
$=SetOption('NoVarsInStrings','On')
Dim $objAdsPath, $obj, $filter[0]
$filter[0] = "User"
$objADsPath = GetObject("LDAP://OU=Normal,OU=Users,OU=SomeOU,DC=mydomain,DC=local")
$objAdsPath.filter = $filter
For Each $obj in $objAdsPath
  ? $obj.CN
Next


This would be the literal translation of your VB Script:
 Code:
$objConnection = CreateObject("ADODB.Connection")
$objCommand = CreateObject("ADODB.Command")
$objConnection.Provider = "ADsDSOObject"
$objConnection.Open("Active Directory Provider")
$objCommand.ActiveConnection = $objConnection

$objCommand.Properties("Page Size").Value = 1000
$objCommand.Properties("Searchscope").Value = 2

$objCommand.CommandText = "SELECT Name FROM 'LDAP://ou=some_ou,dc=subdomain,dc=domain,dc=tld' WHERE objectCategory='user'"  
$objRecordSet = $objCommand.Execute

$objRecordSet.MoveFirst
Do Until $objRecordSet.EOF
  ? $objRecordSet.Fields("Name").Value
  $objRecordSet.MoveNext
Loop

Top
#193658 - 2009-05-04 12:17 PM Re: Getting all users from specific OU [Re: Arend_]
WagnerJu Offline
Fresh Scripter

Registered: 2009-03-20
Posts: 6
Loc: Germany
Hi,
I tried the script:

[quote=apronk]Haven't really read the whole thread by the simplest way to list users in a specific ou would be this:
 Code:
$=SetOption('Explicit','On')
$=SetOption('NoVarsInStrings','On')
Dim $objAdsPath, $obj, $filter[0]
$filter[0] = "User"
$objADsPath = GetObject("LDAP://OU=Normal,OU=Users,OU=SomeOU,DC=mydomain,DC=local")
$objAdsPath.filter = $filter
For Each $obj in $objAdsPath
  ? $obj.CN
Next



but get an error:

ERROR : Error in expression: this type of array not supported in expressions.!
in the line

$objAdsPath.filter = $filter

What am I doing wrong?
I changed the OU and DC paramters in the LDAP-query to match my active directory.

Regards

Jürgen

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 581 anonymous users online.
Newest Members
Audio, Hoschi, Comet, rrosell, PatrickPinto
17880 Registered Users

Generated in 0.148 seconds in which 0.079 seconds were spent on a total of 13 queries. Zlib compression enabled.

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