Sign in to follow this  
Followers 0
Muzza

MoveHere in Active Directory

4 posts in this topic

Hi All,

I am developing a simple app to move computers in AD.

CODE
Local $NewOuObj = ObjGet($strNewOU)

Local $strMvResult = $NewOuObj.MoveHere($strOrgLDapName, "")

Where:

$strNewOU = "LDAP://CN=Computers,DC=mydom,DC=com,DC=au"

and

$strOrgLDapName = "LDAP:\\CN=MG0668,OU=Workstations,DC=mydom,DC=com,DC=au"

On execute i get "The requested action with this object has failed" :-)

I have searched this forum and found a number of ppl asking the same question but none of the solutions work.

Any help greatly appreciated !

thanks,

Muzza

Share this post


Link to post
Share on other sites



@Muzza

Maybe you can do it in 2 steps :

; AddUserToGroup
; Takes the group (SamAccountName without leading 'CN=') and the user (SamAccountName without leading 'CN=')
; Adds the user to the group
; Returns 0 if the user is already a member of the group, 1 if the user was added to the group

Func AddUserToGroup($group, $user)
    If IsMemberOf($group, $user) Then Return 0

    Dim $objConnection, $objRootDSE
    
    $objConnection = ObjCreate("ADODB.Connection")  ; Create COM object to AD
    $objConnection.Provider = "ADsDSOObject"
    $objConnection.Open ("Active Directory Provider")  ; Open connection to AD
    $objRootDSE = ObjGet("LDAP://RootDSE")
    
    Global $strDNSDomain = $objRootDSE.Get ("defaultNamingContext")  ; Retrieve the current AD domain name

    
    $strQuery = "<LDAP://" & $strDNSDomain & ">;(sAMAccountName=" & $user & ");ADsPath;subtree"
    $objRecordSet = $objConnection.Execute ($strQuery)  ; Retrieve the FQDN for the user
    $ldap_entry = $objRecordSet.fields (0).value
    $oUsr = ObjGet($ldap_entry)  ; Retrieve the COM Object for the user
    
    $strQuery = "<LDAP://" & $strDNSDomain & ">;(sAMAccountName=" & $group & ");ADsPath;subtree"
    $objRecordSet = $objConnection.Execute ($strQuery)  ; Retrieve the FQDN for the group
    $ldap_entry = $objRecordSet.fields (0).value
    $oGroup = ObjGet($ldap_entry)  ; Retrieve the COM Object for the group
    
    $OGroup.Add ($oUsr.AdsPath)
    $OGroup.SetInfo
    Return 1
EndFunc   ;==>AddUserToGroup

; RemoveUserFromGroup
; Takes the group (SamAccountName without leading 'CN=') and the user (SamAccountName without leading 'CN=')
; Removes the user from the group (if the user is a member of the group)
; Returns 0 if the user isn't a member of the group, 1 if the user was removed from the group

Func RemoveUserFromGroup($group, $user)
    If IsMemberOf($group, $user) = 0 Then Return 0

    Dim $objConnection, $objRootDSE
    
    $objConnection = ObjCreate("ADODB.Connection")  ; Create COM object to AD
    $objConnection.Provider = "ADsDSOObject"
    $objConnection.Open ("Active Directory Provider")  ; Open connection to AD
    $objRootDSE = ObjGet("LDAP://RootDSE")
    
    Global $strDNSDomain = $objRootDSE.Get ("defaultNamingContext")  ; Retrieve the current AD domain name
    
    $strQuery = "<LDAP://" & $strDNSDomain & ">;(sAMAccountName=" & $user & ");ADsPath;subtree"
    $objRecordSet = $objConnection.Execute ($strQuery)  ; Retrieve the FQDN for the user
    $ldap_entry = $objRecordSet.fields (0).value
    $oUsr = ObjGet($ldap_entry)  ; Retrieve the COM Object for the user
    
    $strQuery = "<LDAP://" & $strDNSDomain & ">;(sAMAccountName=" & $group & ");ADsPath;subtree"
    $objRecordSet = $objConnection.Execute ($strQuery)  ; Retrieve the FQDN for the group
    $ldap_entry = $objRecordSet.fields (0).value
    $oGroup = ObjGet($ldap_entry)  ; Retrieve the COM Object for the group
    
    $oGroup.Remove ($oUsr.AdsPath)
    $OGroup.SetInfo
    Return 1
EndFunc   ;==>RemoveUserFromGroup

regards

ptrex

Share this post


Link to post
Share on other sites

Hi ptrex,

Sorry, I am after assistance moving a PC between 'OU' in AD not group membership. (I will have to remember to state my requests clearer)

But anyway thanks heaps for the code you submitted it will be useful to me as well.

Muzza

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

@Muzza

Maybe something like this ?

; This code moves an object from one location to another in the same domain.

; ------ SCRIPT CONFIGURATION ------
$strNewParentDN = "LDAP://<NewParentDN>"
$strObjectDN    = "LDAP://cn=jsmith,<OldParentDN>"
$strObjectRDN   = "cn=jsmith"
; ------ END CONFIGURATION ---------

$objCont = ObjGet($strNewParentDN)
$objCont.MoveHere $strObjectDN, $strObjectRDNoÝ÷ Ù8b²j/y©e
©º×«²)ÚåºÚ"µÍÌÍÛØ]ÓÕHHØÙ]
    ][ÝÓTËÛÝOY[[ÙKÏYXZØ[KÏXÛÛI][ÝÊBÌÍÛØÛÕHHØÙ]
    ][ÝÓTËÛÝOXXØÛÝ[[ËÏYXZØ[KÏXÛÛI][ÝÊBÌÍÛØÛÕK[H   ÌÍÐ^J    ][ÝÐÛÛ]][ÝÊBÜ    ÌÍÛØÙ[ ÌÍÛØÛÕB   ÌÍÛØ]ÓÕK[ÝRH
    ÌÍÛØÙQÔ]Y][
B^

regards

ptrex

Edited by ptrex

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0