Please review this rewrite of Translate and comment. This is a precursor to a rewrite of InContainer(). This new TranslateName() UDF return all the desired name types with one bind to AD. The intent would be to execute this UDF once at the top of your script and save the names in a global array. You then can use one or more of the name types anywhere in the remaining code.

The UDFs like InContainer() and MakePathsFromADlocation() will no longer call TranslateName. They will be rewritten to accept the string represention of the name (Canonical or Distinguished) and not depend on an internal call to TranslateName().
Code:
$rc=SetOption("explicit","ON")

$Names = TranslateName (3, "", 3, @Domain + "\" + @wksta + "$", "1,2,3,4,5,6,7,8,9,10,11,12")
Dim $x
? "Ubound = " + ubound($names)
for $x=0 to ubound($Names)
? ""+ $x +": " + $Names[$x]
next
?
$Names = TranslateName (3, "", 3, @Ldomain + "\" + @userID, "1,2,3,4,5,6,7,8,9,10,11,12")
Dim $x
? "Ubound = " + ubound($names)
for $x=0 to ubound($Names)
? ""+ $x +": " + $Names[$x]
next
?
$Names = TranslateName (3, "", 3, @LDomain + "\" + @userid, 1)
? "DN = " + $Names[1]


;--------------------------------------------------------------------------------------------------
;FUNCTION TranslateName()
;
;AUTHOR Howard A. Bullock (hbullock@tycoelectronics.com)
;
;VERSION 3.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)
;
;DATE 2005/04/22
;
;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 that the object will bind to.
;
; $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)
; - single Integer or comma separated string of numbers
;
; Documentation of Name Types. Lookup the more info on http://MSDN.Microsoft.com
; Not all name types work. The two that have been most useful to are "1" and "3"
;
; 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 all name types seem to work. $ReturnNameType can be a single number or a comma separated
; list of numbers. The resulting array will have the coresponding array elements populated
; with the specific names requested.
;
;RETURNS This function returns a 13 element ARRAY. Use the array element that corresponds to the
; number of the name type listed above.
;
;DEPENDENCIES OS: Active Directory aware client
;
;EXAMPLES
;
; These two examples attempt to retrieve all names
; $Names = TranslateName (3, "", 3, "@Domain\@wksta$", "1,2,3,4,5,6,7,8,9,10,11,12")
; Dim $x
; ? "Ubound = " + ubound($names)
; for $x=0 to ubound($Names)
; ? ""+ $x +": " + $Names[$x]
; next
; ?
; $Names = TranslateName (3, "", 3, "@LDomain\@userid", "1,2,3,4,5,6,7,8,9,10,11,12")
; Dim $x
; ? "Ubound = " + ubound($names)
; for $x=0 to ubound($Names)
; ? ""+ $x +": " + $Names[$x]
; next
;
; Get Distinguished Name (Name type #1)
; $Names = TranslateName (3, "", 3, "@LDomain\@userid", 1)
; ? "DN = " + $Names[1]
;
;
Function TranslateName ($InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType)
Dim $NameTranslate, $Error, $ErrorText, $ReturnNameArray[12], $GetNameArray, $value

$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
Dim $index
$GetNameArray = split($ReturnNameType, ",")
for $index=0 to ubound($GetNameArray)
$value = $GetNameArray[$index]
$ReturnNameArray[$value] = $NameTranslate.Get($value)
next
else
exit $Error
endif
else
exit $Error
endif
else
exit $Error
endif
$TranslateName = $ReturnNameArray
Endfunction



Edited by Howard Bullock (2005-04-23 03:33 PM)