Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Win32::OLE Password Change?

by jpavel (Sexton)
on Mar 21, 2003 at 20:24 UTC ( [id://244997]=perlquestion: print w/replies, xml ) Need Help??

jpavel has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks - For all the OLE guru's... I'm trying to change a user's password through OLE. I want to keep my code consistent in using OLE instead of resorting to Win32::AdminMisc. For the rest of my code, I bind to WMI with the following:
@host = ($server,"root/cimv2",$user,$password); $localWMI = Win32::OLE->new('WbemScripting.SWbemLocator') or die "Cann +ot access WMI on local machine: ", Win32::OLE->LastError; $WMI = $localWMI->ConnectServer(@host) or die "Cannot access WMI on re +mote machine: ", Win32::OLE->LastError;
I want to be able to do a similar bind to WINNT where I can use an alternate moniker than what I'm logged in with (as above). The only way I know to change a password using OLE is:
if ($myDomain = Win32::OLE->GetObject("WinNT://$server/$pworduser")) { $result=$myDomain->ChangePassword($oldpassword, $newpassword); print "Result: $result\n"; } else { print "Couldn't connect :-(\n"; }
But this only uses the logged in credentials as a moniker. Is there a host array I can use to supply different credentials to connect to WINNT instead of root\cimv2? Or is there another way I can change a password of a user (it doesn't seem as if the Win32_User object has any methods associated with it unforuntately...). All my attempts to connect to WINNT using scripting or wbemtest.exe have results in "Invalid namespace" failures. Any thoughts or comments are appreciated!

Replies are listed 'Best First'.
Re: Win32::OLE Password Change?
by blm (Hermit) on Mar 22, 2003 at 11:53 UTC

    You seem a little confused but then I may be misunderstanding. The first example fragment uses WMI and the second uses ADSI through the NT namespace WinNT:://. root\cimv2 is not a user it is a namespace or context(?) for WMI. You don't need to connect with WMI to use ADSI. Also in the code:

    $myDomain = Win32::OLE->GetObject("WinNT://$server/$pworduser")

    The code returns a user object not a domain object. $pworduser is not the username of the account that you are using to establish privilege to alter user accounts. It should probably be something like:

    $objUser = Win32::OLE->GetObject("WinNT://$server/$pworduser")

    Look at Problems with Win32::OLE and ADSI in which rob_au gives code that uses an alternative username and password to connect to ADSI. Once connected you can change people's passwords with

    $objUser->SetPassword($password);

    When you say you are having troubles connecting to WinNT with "Invalid namespace" errors is that in the second code fragment? What NT version are you using? If you are using NT4 you may need to install Active Directory Service Interfaces for Microsoft Windows NT Server 4.0

    Unfortunately I am making assumptions and I can't test this at home as I haven't the right platforms.

      That was a huge help! I think I misspoke.... I knew I was attempting to bind to ADSI and not WMI in the second snippet. My problem was in using alternate credentials for an OLE ADSI binding.

      In case anyone reads this in the future and would like my resolution, here it is:

      $winntobj = Win32::OLE->GetObject('WinNT:') or croak( 'Cannot create WINNT object - ', $! ); if ($debug) { print "Attempting bind to WINNT on $server for authenticating $use +r/$password...\n"; } $userobj = $winntobj->OpenDSObject( "WinNT://$server/$pworduser,User", $user, $password, 1 ); my $err = Win32::OLE->LastError(); if ($err == 0) { print STDOUT "Successful authentication!\n"; else { print STDOUT $err; } $userobj->SetPassword($newpassword);
      ...and this works as long as the authenticated user has administrative rights. I'd still be interested in knowing if anyone has a WMI solution for this as well, but I am very happy using ADSI. Thanks again!!!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-23 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found