http://qs321.pair.com?node_id=411282

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

Hi Monks,
I am trying to find a way to check if a user exists in specific Originization Unit. I cannot use a typical if statement on my list of users:


$path='cn=username,ou=mybase_ou,dc=computer,dc=com'; if (my $objUser = Win32::OLE->GetObject("LDAP://" . $path)){ }



Because the program will die as soon as I reach a username that does not exists, because the bind fails. Any help or suggestions would be much appreciated
%^+^%

Replies are listed 'Best First'.
Re: AD User Exist?
by boo_radley (Parson) on Nov 30, 2004 at 21:12 UTC
    When I had to do this, I did it with Win32::AD::User.
    $user=Win32::AD::User->new("WinNT://DOMAIN/username,user","username"); $user->get_info(); print join "\n",$user->get_groups();
    if $user's undef, there's no account there.
Re: AD User Exist?
by VSarkiss (Monsignor) on Nov 30, 2004 at 21:55 UTC
      Thank you, Eval worked perfectly.
Re: AD User Exist?
by Stevie-O (Friar) on Dec 01, 2004 at 00:29 UTC
    My suggestion is similar to VSarkiss's:
    my $objUser = eval { Win32::OLE->GetObject("LDAP://$path") }; # now, $objUser will either be an object (if it worked), or # undef (if it didn't work).
    --Stevie-O
    $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
Re: AD User Exist?
by ekkis (Initiate) on Nov 30, 2004 at 21:09 UTC
    wrap the stmt in a try?