Swapping attributes in AD

398 views Asked by At
on error resume next

Set objUser = GetObject("LDAP://CN=" & sAMAccountName & _
              "ou=usuarios,dc=quahog,dc=edu,dc=br")
do while objUser = true
       objUser.Put "objUser.otherMailbox", "objUser.mail"
       objUser.Put "objUser.mail", "objUser.userPrincipalName"
loop

wscript.echo "Done"

I am trying to swap some attributes in my Active Directory: I want my actual e-mail attribute, which happens to be mail, to be my otherMailbox. Then, I want my actual logon user name, which I believe to be objUser.userPrincipalName, to be also my new e-mail.

2

There are 2 answers

0
Matheus On
    Set objUser = GetObject("LDAP://CN=Peter Griffin,ou=usuarios,dc=quahog,dc=edu,dc=br")

    objUser.Put "otherMailbox", objUser.mail
        objUser.Put "mail", objUser.userPrincipalName
    objUser.SetInfo


wscript.echo "done"

The code above works perfectly, but if I change CN to sAMAccountName I get this error: I got this error:

  • Line: 1
  • Caract.: 2
  • Error: 0x80005000
  • Code: 80005000
  • Source: (null)
0
Ansgar Wiechers On

VBScript debugging step 1: remove On Error Resume Next and see what breaks.

AFAICS your code has 3 issues (aside from the OERN):

  • The loop. objUser is an object, not a boolean, so objUser = True will fail.
  • The Put method expects the attribute name as the first parameter and the new value for the attribute as the second.
  • After changing an attribute you need to actually write the changes back to AD.

Change your code to this:

Set objUser = GetObject("LDAP://CN="& sAMAccountName & _
              "ou=usuarios,dc=quahog,dc=edu,dc=br")

objUser.Put "otherMailbox", objUser.mail
objUser.Put "mail", objUser.userPrincipalName
objUser.SetInfo

WScript.Echo "Done"

Check out the ActiveXperts script repository for more examples.