Link to home
Start Free TrialLog in
Avatar of YZlat
YZlatFlag for United States of America

asked on

Move user to another OU with different credentials

I am writing the script that moves currently logged in user to a different OU

here is the script to do that

Set objSysInfo = CreateObject("ADSystemInfo") 
strUser = objSysInfo.UserName 
Set objUser = GetObject("LDAP://" & strUser) 
objuser.MoveHere("LDAP://OU=MyTestOU,DC=MyDomain,DC=net")

Open in new window


but I need to authenticate using service account first, since regular account would not have permissions to move AD object. So here is my updated code:

 
Set objSysInfo = CreateObject("ADSystemInfo") 
strUser = objSysInfo.UserName 
Set oNamesp = GetObject("LDAP:")
Set objUser = oNamesp.OpenDSObject("LDAP://" & strUser, "TestUser", "TestPwd",1)
objuser.MoveHere("LDAP://OU=MyTestOU,DC=MyDomain,DC=net")

Open in new window


but I am still getting "Access denied" error, even though when I open Active directory logged in as TestUser, I am able to manually move user objects.

Can anyone tell me what is wrong here?
Avatar of jorgedeoliveiraborges
jorgedeoliveiraborges
Flag of Brazil image

I found this on www. Please, take a look at ...

...
«I tested the script below (with a different target OU) and it moved the
computer the script was run on into the target OU. Watch for line wrapping»
:====================

Option Explicit
Dim objNewOU, objSysInfo, strComputerDN, objMoveComputer

' Bind to target OU, where computer object will be moved.
Set objNewOU = GetObject("LDAP://OU=Sales,dc=MyDomain,dc=com")

' Determine Distinguished Name of local computer.
Set objSysInfo = CreateObject("ADSystemInfo")
strComputerDN = objSysInfo.ComputerName

' Move this computer object to the target OU.
Set objMoveComputer = objNewOU.MoveHere("LDAP://" & strComputerDN, 
vbNullString)

Open in new window


http://www.winserverkb.com/Uwe/Forum.aspx/windows-server-scripting/9358/Move-Computer-from-default-OU-to-another-during
Avatar of RobSampson
Hi, try this.

Regards,

Rob.
sADDomain = "yourdomain"
sADUser = "adminuser"
sADPassword = "adminpassword"

Const ADS_SECURE_AUTHENTICATION = 1
sDestOU = "LDAP://OU=targetOU,OU=sites,DC=domain,DC=com"
Set objRootDSE = GetObject("LDAP:")
Set objSysInfo = CreateObject("ADSystemInfo")
Set objDestOU = objRootDSE.OpenDSObject(sDestOU, sADDomain & "\" & sADUser, sADPassword, ADS_SECURE_AUTHENTICATION)
Set objUser = objRootDSE.OpenDSObject("LDAP://" & objSysInfo.UserName, sADDomain & "\" & sADUser, sADPassword, ADS_SECURE_AUTHENTICATION)
On Error Resume Next
objDestOU.MoveHere "LDAP://" & objUser.distinguishedName, vbNullString
If Err.Number = 0 Then
	MsgBox "User moved successfully."
Else
	MsgBox "Error " & Err.Number & ": " & Err.Description
	Err.Clear
End If

Open in new window

Avatar of YZlat

ASKER

jorgedeoliveiraborges, your code is pretty much the same thing I posted saying it does not work for me because the logged in user needs to have permissions to move the object
Avatar of YZlat

ASKER

RobSampson, if you read my question carefully, you will see that you posted the code identical to the code i used when I got Access denied error
Not quite.  In mine, it uses OpenDSObject to bind to both the OU and the User, while yours only binds to the user.

And, if you read my code carefully, and compare it to yours, you will see that you are performing the move the wrong way around, by trying to move the user into the OU, whereas it is actually done by pulling the user into the OU.

Rob.
Avatar of YZlat

ASKER

OK, I see.

Rob, I am not sure what does this line do:

objDestOU.MoveHere "LDAP://" & objUser.distinguishedName, vbNullString

Avatar of YZlat

ASKER

objDestOU.MoveHere "LDAP://" & objUser.distinguishedName, vbNullString

gives me an error "An invalid directory path was passed"

I checked objUser.distinguishedName and it is correct

I think the above statement attempts to do things in the wrong order - trying to move OU to a user as opposed to moving the user to an OU
How can you move an OU to a user?  A user is not a container that can hold other objects.  In ADUC when you drag a user object into an OU, the OU stays where it is, and the user moves into it.

Above this:
objDestOU.MoveHere "LDAP://" & objUser.distinguishedName, vbNullString

if you put this
MsgBox objUser.distinguishedName

if it outputs the distinguished name then you know it binds to the user object correctly.

You could also add
MsgBox objDestOU.distinguishedName

so you know whether the OU bind was successful too.

Regards,

Rob.
Avatar of YZlat

ASKER

I ckecked both and both are correct, but
objDestOU.MoveHere "LDAP://" & objUser.distinguishedName, vbNullString

gives me an error "An invalid directory path was passed"

objDestOU is an ou LDAP://OU=LDAP://OU=MyTestOU,DC=MyDomain,DC=net

and objUser.distinguishedName if LDAP://CN=MyUser,OU=AnotherOU,DC=MyDomain,DC=net



ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of YZlat

ASKER

RobSampson, correct me if I am wrong, but I believe 9and that's what I see when searching the internet as well), but the syntax for MoveHere is

User.MoveHere OU, vbNullString

not

OU.MoveHere User, vbNullString

in your statement

objDestOU.MoveHere "LDAP://" & objUser.distinguishedName, vbNullString

I believe you are reversing the user and OU
Avatar of YZlat

ASKER

RobSampson, I think your latest code worked!
I hope so  ;-)
Avatar of YZlat

ASKER

here is a strange occurrence: When I run your VBScript from a separate vbs file, it works, but if I put the code inside my hta, I get in this line:
Set objDestOU = objRootDSE.OpenDSObject(sDestOU, sADDomain & "\" & sADUser, sADPassword, ADS_SECURE_AUTHENTICATION)
 "Unknown username or bad password"
Avatar of YZlat

ASKER

Trns out it was just a typo.

Thank a lot!
Good to hear. Thanks for the grade.

Checking the MSDN articles on methods is something I do every day to make sure I get the syntax of commands right.  Often they have good examples as well, although sometimes not, which is annoying.

Regards,

Rob.