;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       GetObjectOU() 
;; 
;;ACTION         Returns the OU string for the current user (default) or computer 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION	 1.2  - 2009/08/10 
;; 
;;HISTORY        1.0  - 2006/12/06 - Initial Release 
;;		 1.1  - 2008/07/20 - Changed return method to properly return element 0 
;;               1.2  - 2009/08/10 - Added support to return info about the Computer OU 
;; 
;;SYNTAX         GetUserOU([Object] [, Computer]) 
;; 
;;PARAMETERS     Object - OPTIONAL - String 
;;               - Specify a particular object ID to return 
;; 
;;		 Computer - OPTIONAL - String 
;;               - Binary flag (0/1) - returns computer info when set 
;; 
;;REMARKS        Initial development for use in the Universal Login Script 
;; 
;;RETURNS        String - comma-delimited OU list - OU=x,OU=y,DC=dom,DC=local 
;;               or, with Object specified, the specific object without the qualifier 
;;               eg: GetUserOU(1) returns "y" instead of "OU=x,OU=y,DC=dom,DC=local" 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3 
;; 
;;EXAMPLES       If GetObjectOU(1) = "My Dept"         ; user is in "My Dept" OU 
;;                 Use E: \\server\share 
;;               EndIF 
;;	         If GetObjectOU(0,1) = "Workstations"  ; computer is in Workstations OU 
;;                 Use F: \\server\share 
;;               EndIF 
; 
Function GetObjectOU(OPTIONAL $_Object, OPTIONAL $_Computer)
 
 
  Dim $_aTemp					; array of DN data 
  Dim $_Work					; output string 
  Dim $_ADSys					; ADSI object pointer 
  Dim $_I, $_D					; index vars 
 
  $_D = ''
 
  $_ADSys = CreateObject('ADSystemInfo')
  If @ERROR Exit @ERROR EndIf			; exit if error creating object 
  If $_Computer
    $_aTemp = $_ADSys.ComputerName
  Else
    $_aTemp = $_ADSys.UserName
  EndIf
  If @ERROR Exit @ERROR EndIf			; exit if error obtaining data 
 
  ; Handle a "Last, First" situation - just remove the "\," as it won't be needed 
  $_aTemp = Join(Split($_aTemp, '\,'), '')
 
  ; Split the remaining OU and DC components into an array 
  $_aTemp = Split($_aTemp, ',')
 
  ; Return only OU and DC info - build the string 
  For Each $_I in $_aTemp
    If Not InStr($_I, 'CN=')
      $_Work = $_Work + $_D + $_I
      $_D = ','
    EndIf
  Next
 
  ; return the string, or the specific object requested 
  $GetObjectOU = $_Work
  If VarType($_Object)
    $GetObjectOU = SubStr(Split($_Work, ',')[$_Object], 4)
  EndIf
 
  Exit 0
 
EndFunction