;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       MoveOU() 
;; 
;;ACTION         Moves an object from one OU path to another. 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.1  - 2017/09/15 
;; 
;;HISTORY        1.0  - 2013/08/05 - Initial Release 
;;               1.1  - 2017/09/15 - added specific return value - 1=success, 0=failure 
;; 
;;SYNTAX         MoveOU(ObjectName, SourceDN, TargetDN) 
;; 
;;PARAMETERS     ObjectName - Required - String 
;;               - The name of the object, with or without the leading "CN=" 
;; 
;;               SourceDN - Required - String 
;;               - The DN string referencing the source OU 
;; 
;;               TargetDN - Required -String 
;;               - the DN string referencing the target OU 
;; 
;;REMARKS        Only operates on individual (leaf) objects. The source and target 
;;               DN strings do not need to specify the "ldap://" prefix. 
;; 
;;RETURNS        nothing 
;; 
;;DEPENDENCIES   COM 
;; 
;;TESTED WITH    W2K, WXP, W2K3, w2k8, W2K12, W7 
;; 
;;EXAMPLES       ; Move a PC object from remote Site1 to Headquarters 
;;		 $Src = 'OU=Computers,OU=Site1,DC=fabricam,DC=com' 
;;		 $Dst = 'OU=Computers,OU=Headquarters,DC=fabricam,DC=com' 
;;		 $RC = MoveOU('TestPC01', $Src, $Dst) 
; 
; move the named Object from a source to target OU, specifying the DN string 
Function MoveOU($_ObjName, $_SrcDN, $_TgtDN)
 
  Dim $_				; throwaway var 
  Dim $_oDestOU				; object reference for Destination OU 
 
  ; insure that the object name begins with "CN=" 
  If Left($_ObjName, 3) <> 'CN='
    $_ObjName = 'CN=' + $_ObjName
  EndIf
 
  ; strip leading "LDAP://" from source and target DN arguments. 
  If Left($_SrcDN, 7) = 'LDAP://'
    $_SrcDN = SubStr($_SrcDN, 8)
  EndIf
  If Left($_TgtDN, 7) = 'LDAP://'
    $_TgtDN = SubStr($_TgtDN, 8)
  EndIf
 
  ; instantiate the target OU, then use MoveHere to specify the source and 
  ; execute the move 
  $_oDestOU = GetObject('LDAP://' + $_TgtDN)
  $_ = $_oDestOU.MoveHere('LDAP://' + $_ObjName + ',' + $_SrcDN, $_ObjName)
 
  $MoveOU = Not @ERROR			; return Success/Fail value 
  Exit @ERROR				; exit with error status 
 
EndFunction