Here's the output from my script:
Leave SECRETARY in Admin
New OU is LDAP://OU=Technology,OU=DomainComputers,DC=xxx,DC=d2g,DC=com
dest DN is OU=Technology,OU=DomainComputers,DC=xxx,DC=d2g,DC=com
from DN is CN=STUDENT100,OU=Private,OU=DomainComputers,DC=xxx,DC=d2g,DC=com
Move STUDENT100 from Private to Technology
error 80005000
Leave LAP6514H in Kiosk
Leave LAP6461H in Teachers
And here's the offending code. This is inside a loop which reads ou1 from a database of new destinations, ou2 is read from AD for the corresponding computer.
If ou1="" Then
wscript.echo "Leave",computername,"in",ou2
Else
If ou1<>ou2 Then
'move to right OU
newOU = "LDAP://OU=" & ou1 & ",OU=DomainComputers," & DCsuffix
wscript.echo "New OU is",newOU
Set objOU = GetObject(newOU)
wscript.echo "dest DN is",objOU.DistinguishedName
wscript.echo "from DN is",PCDN
Wscript.echo "Move",computername,"from",ou2,"to",ou1
On Error Resume next
objOU.MoveHere PCDN, vbNullString
If err.number<>0 then wscript.echo "error",Hex(err.number)
On Error GoTo 0
Set objOU = Nothing
End If
End If
I have almost identical code elsewhere in the same script which moves users to new OUs successfully with obj.MoveHere
I have used ADSI edit to check the OUs are right, and the output shows that the new ou exists. I have checked permissions on the containers. I can move the computers with the AD Users and Computers MMC. I run the script as administrator on a Win2012 R2 DC. I tried the other DC. If I remove the 'On Error resume' it shows the text "Active Directory: An invalid directory pathname was passed" and stops. Can someone spot my error please?
2 Mar: I have run out of things to try. As a workaround, I use the script (with the On Error...) to see what needs moving, then use the AD MMC to move them manually. I'd appreciate a better suggestion. Is there perhaps another way to move an AD object not using LDAP?