;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       ADSIUserInfo() 
;; 
;;ACTION         Get or Set a user attribute via ADSI 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        2.0 - 2010/01/11 
;; 
;;               1.0 - 2008/03/05 - Initial Release 
;;               1.1 - 2008/08/20 - Modified to return current UserDN when no args are specified 
;;               2.0 - 2010/01/11 - Modified to use an array or individual value for read/write 
;; 
;;SYNTAX         ADSIUserInfo([User] [, Attribute] [, Value]) 
;; 
;;PARAMETERS     User - OPTIONAL - AD Distinguished name. Defaults to current user 
;; 
;;               Attribute - OPTIONAL - Attribute value to read or set. If User and 
;;               Attribute are both null, the current user's DN will be returned. If User is 
;;               specified and Attribute is null, and error is returned 
;; 
;;               Value - OPTIONAL - Value to set 
;; 
;;REMARKS        See ADSIObjectInfo() for updated functionality 
;;               Returns AD attribute value (including what was just set) 
;;               Sets an AD attribute value 
;; 
;;RETURNS        String - value of defined attribute or UserDN if no args are specified 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3, Vista 
;; 
;;EXAMPLES       None 
; 
Function ADSIUserInfo(OPTIONAL $_User, OPTIONAL $_Attr, OPTIONAL $_Value)
 
  Dim $_				; temporary var 
  Dim $_I				; index var 
  Dim $_AValid				; valid array data flag 
  Dim $_D				; "$" character 
  Dim $_oUser				; User object pointer 
  Dim $_Cmd				; execute command string 
  Dim $_RtnVal				; data returned from Execute 
  Dim $_objSysInfo 			; object pointer 
 
  $_D = Chr(36)
 
  ; Determine user DN if not supplied 
  If Not $_User
    $_objSysInfo = CreateObject("ADSystemInfo")
    $_User = $_objSysInfo.UserName
    $_ = 1
    If @ERROR Exit @ERROR EndIf
    ; Return the DN if no attribute was specified 
    If Not $_Attr
      $ADSIUserInfo = $_User
      Exit 0
    EndIf
  EndIf
 
  ; attribute is required if DN is specified 
  ; Exit with error if Attribute is null 
  If VarType($_Attr) < 2
    Exit 87
  EndIf
 
  If VarType($_Attr) = 8
   If Not $_Attr Exit 87 EndIf
  EndIf
 
  If Left($_User, 7) <> 'LDAP://'
    $_User = 'LDAP://' + $_User
  EndIf
 
  ; Get the user pointer 
  $_oUser = GetObject($_User)
  If @ERROR Exit @ERROR EndIf			; exit if error creating object 
 
  If VarType($_Attr) < 8192
    ; write the value if supplied 
    If $_Value
      $_Cmd = $_D + '_oUser.put("' + $_Attr + '", "' + $_Value + '")'
      $_ = Execute($_Cmd)
      $_oUser.SetInfo
      If @ERROR
        Exit @ERROR
      EndIf
    EndIf
 
    ; read and return the current value 
    $_Cmd = $_D + '_RtnVal = ' + $_D + '_oUser.' + $_Attr
    $_ = Execute($_Cmd)
    $ADSIUserInfo = $_RtnVal
 
  Else
    $_AValid = 0
    Dim $_RtnVal[UBound($_Attr)]		; prepare to return multiple values	 
    For $_I = 0 to UBound($_Attr)
      If $_Attr[$_I]
        $_AValid = 1
        ; write the value if supplied 
        If $_Value[$_I]
          $_Cmd = $_D + '_oUser.put("' + $_Attr[$_I] + '", "' + $_Value[$_I] + '")'
          $_ = Execute($_Cmd)
          $_oUser.SetInfo
          If @ERROR
            Exit @ERROR
          EndIf
        EndIf
 
       ; read and return the current value 
       $_Cmd = $_D + '_RtnVal[$_I] = ' + $_D + '_oUser.' + $_Attr[$_I]
       $_ = Execute($_Cmd)
 
      EndIf
    Next
    $ADSIUserInfo = $_RtnVal
    If Not $_AValid Exit 87 EndIf		; no valid values 
  EndIf
 
  Exit @ERROR
 
 
EndFunction