Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Active Directory update weirdness

by Sue D. Nymme (Monk)
on Jul 26, 2010 at 14:39 UTC ( [id://851372]=perlquestion: print w/replies, xml ) Need Help??

Sue D. Nymme has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I am trying to update user information in Active Directory via Win32::OLE, and I am seeing some behavior that confuses me.

Consider the following short program:

# ---------------------------------------------------------------- use Modern::Perl; no warnings 'uninitialized'; use Win32::OLE; my $ADsPath = 'LDAP://CN=Guest11,OU=OurDept,DC=OurDomain,DC=com'; my $user = Win32::OLE->GetObject($ADsPath); # Some simple info say 'Initial fetch'; my $uid = $user->sAMAccountName; my $name = $user->Name; my $phone = $user->telephoneNumber; say " uid=$uid, name=$name, phone=$phone\n"; # This prints: uid=guest11, name=CN=Guest11, phone=000 # Update $user->{telephoneNumber} = '555'; $user->SetInfo; # ****************** # Re-display. say 'After update'; $uid = $user->sAMAccountName; $name = $user->Name; $phone = $user->telephoneNumber; say " uid=$uid, name=$name, phone=$phone\n"; # This prints: uid=guest11, name=CN=Guest11, phone=Guest11 # ****************** # Re-Fetch. say 'Re-fetch'; $user = Win32::OLE->GetObject($ADsPath); $uid = $user->sAMAccountName; $name = $user->Name; $phone = $user->telephoneNumber; say " uid=$uid, name=$name, phone=$phone"; # This prints: uid=guest11, name=CN=Guest11, phone=555 # ---------------------------------------------------------------

This program
    fetches a user object from AD,
    prints three fields,
    updates one of the fields,
    prints the three fields again,
    fetches the same object,
    prints the three fields again.

Note the section that is set off with asterisks. After I change one field's value and call SetInfo, the field appears to become corrupt. In this case, it now holds the value of the displayName AD field. In other cases, it prints "66048", which happens to be the value of the userAccountControl AD field. The value that it prints seems to be unpredictable.

Yet when I fetch the object again, it contains the correct value. And when I look at the value in Active Directory, it is set properly, and nothing seems to be corrupted.

What's going on here? Is a fetch always needed after a call to SetInfo?

Replies are listed 'Best First'.
Re: Active Directory update weirdness
by NetWallah (Canon) on Jul 26, 2010 at 19:52 UTC
    I would use Net::LDAP for this kind of thing so I'm not familiar with this usage, but here are some suggestions:
    • ERROR Checking after each fetch/update: Something like :
      If (Win32::OLE->LastError != 0) {
      print "objuser (or whatever): ".Win32::OLE->LastError()
    • Use the "Put" function instead of hash access : $user->Put("telephoneNumber","555");
    • Search this site for similar usage, like this node

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

      Thanks for your reply.

      • I had omitted error-checking for brevity. At each point, however, LastError() was returning 0.
      • Put didn't help; same behavior.
      • I had been shying away from Net::LDAP, simply because there seemed to be even less documentation around for it than Win32::OLE and ASDI (although they're both pretty sparse, alas). But the script in the replies to that comment did work, and did help me along. Thank you!
Re: Active Directory update weirdness
by bingos (Vicar) on Jul 27, 2010 at 19:35 UTC

    I may be missing something, but I can't see a call to GetInfo to populate the property cache before you call SetInfo

      No, I'm afraid that inserting GetInfo() at various points in the test script above had no effect. It still printed a random garbage field in the "After update" section.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://851372]
Approved by Corion
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-16 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found