;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       GetObjectDN() 
;; 
;;ACTION         Returns an LDAP DN string for a computer, user, or group object 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0 - 2011/12/28 
;; 
;;HISTORY        1.0 - 2011/12/28 - Initial Release 
;; 
;;SYNTAX         GetObjectDN([Object_Name]) 
;; 
;;PARAMETERS     Object - OPTIONAL - String 
;;               - An AD user or group name in DOMAIN\name format. If DOMAIN\ is not 
;;		 specified, the local domain is used. If no object name is specified, 
;;		 the current user is used. To return a DN for a computer, the computer 
;;		 name must be terminated with "$" 
;; 
;;REMARKS        Many AD functions require a DN string to reference a user or group. 
;;		 This UDF provides a quick way to return a DN string for most common 
;;               objects. 
;; 
;;RETURNS        DN String for the specified object 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3, W2K8, Win7 
;; 
;;EXAMPLES       $DN = GetObjectDN('DOMAIN\useriD') 
; 
Function GetObjectDN($_Name)
 
  Dim $_ADS_NAME_INITTYPE_GC			; NameTranslate contstant 
  Dim $_ADS_NAME_TYPE_NT4			; NameTranslate contstant 
  Dim $_ADS_NAME_TYPE_1779			; NameTranslate contstant 
  Dim $_objTrans				; Object Pointer 
  Dim $_Err					; Error var 
 
  $_ADS_NAME_INITTYPE_GC = 3
  $_ADS_NAME_TYPE_NT4    = 3
  $_ADS_NAME_TYPE_1779   = 1
  $_Err                  = 0			; no error 
 
  If Not $_Name					; default to current user 
    $_Name = @DOMAIN + '\' + @USERID
  EndIf
 
  If Not InStr($_Name, '\')			; default to current domain if not specified 
    $_Name = @DOMAIN + '\' + $_Name
  EndIf
 
  ; Use the NameTranslate object to convert the NT user name to the Distinguished Name  
  $_objTrans = CreateObject('NameTranslate')
  $_Err = IIf(@ERROR, @ERROR, $_Err)			; retain the error 
 
  ; Initialize NameTranslate by locating the Global Catalog 
  $_objTrans.Init($_ADS_NAME_INITTYPE_GC,'')
  $_Err = IIf(@ERROR, @ERROR, $_Err)			; retain the error 
 
  ; Use the Set method to specify the NT format of the object name 
  $_objTrans.Set($_ADS_NAME_TYPE_NT4, $_Name)
  $_Err = IIf(@ERROR, @ERROR, $_Err)			; retain the error 
 
  ; Use the Get method to retrieve the RFC 1779 DN 
  $_Name = $_objTrans.Get($_ADS_NAME_TYPE_1779)
 
  $GetObjectDN = $_Name
  Exit $_Err
 
EndFunction