With AD, you probaly need to get "telephoneNumber" instead of
"TelephoneNumber".

But I think you're going about it the long way round perhaps.  One way
to do it would be as follows (note you'll have to provide the
authentication parameters):

    use Win32::OLE;

    my $user = $ARGV[0];

    $ADroot = Win32::OLE->GetObject("LDAP://rootDSE";);
    $NC = $ADroot->Get("defaultNamingContext");
    $DC = $ADroot->Get("DnsHostName");

        print "$NC\n";
        print "$DC\n";

    $adsiroot = "<LDAP://$DC/$NC>";
    $ldapfilt =
"(&(objectCategory=person)(objectClass=User)(cn=$user))";
    $attrib = "displayName,telephoneNumber";
    $scope = "subtree";               

    $connObj = Win32::OLE->new("ADODB.Connection");
    $cmdObj = Win32::OLE->new("ADODB.Command");
    $RS = Win32::OLE->new("ADODB.Recordset");
    $connObj->{Provider} = "ADSDSOObject";
    $connObj->Properties("User ID")->{Value} = "$domain\\$admin";
    $connObj->Properties("Password")->{Value} = $pass;
    $connObj->Properties("Encrypt Password")->{Value} = '1';
    $connObj->Open("ADs Provider");

    $cmdObj->{ActiveConnection} = $connObj;
    $cmdObj->{CommandText} = ("$adsiroot;$ldapfilt;$attrib;$scope");

    $RS = $cmdObj->Execute;

    if(!$RS)
    {
        my $Errors = $connObj->Errors();
        #print "Error: $Errors\n";
        foreach my $error (keys %$Errors)
        {
            print "Error: $error->{Description}\n";
        }
        exit;
    }


    $RS->MoveLast;
    $RS->MoveFirst;

    print $RS->RecordCount . " matches found.\n";

    while ( !$RS->{EOF} )
    {
        my ($name,$phone) = (
                $RS->Fields('displayName')->{value},
                $RS->Fields('telephoneNumber')->{value} );

        print "$name\t$phone\n" if ( $phone ne "" );

        $RS->MoveNext;
    }

    $RS->Close;
    $cmdObj->Close;
    $connObj->Close;

    exit;

If you know what container the user is in, it's even easier:

    $ADroot = Win32::OLE->GetObject("LDAP://rootDSE";);
    $NC = $ADroot->Get("defaultNamingContext");
    $DC = $ADroot->Get("DnsHostName");

    my $objAD = Win32::OLE->GetObject("LDAP:");

    # "CN=Users" is the container here
    $objPath = "LDAP://CN=$user,CN=Users,$DC";;

    # Have to know the user id and password to auth to AD, or be logged
in as admin
    $objUser = $objAD->OpenDSObject("$objPath","$ldapid","$ldappass",1);

    @LDAPnames = ("displayName","telephoneNumber");
    $objUser->GetInfoEx(@LDAPnames,0);
    my $name = $objUser->Get("displayName");
    my $phone = $objUser->Get("telephoneNumber");
    print "$name\t$phone\n" if ( $phone ne "" );

For updates, look into the documentation on PutEx in MSDN.


=======================================================
Andy Webb            [EMAIL PROTECTED]      www.swinc.com
Simpler-Webb, Inc.   Austin, TX            512-322-0071
======================================================= 

-----Original Message-----
From: Norris, Joseph [mailto:[EMAIL PROTECTED]] 
Posted At: Friday, September 27, 2002 3:27 PM
Posted To: perl-win32-admin
Conversation: Active Directory question
Subject: FW: Active Directory question




-----Original Message-----
From: Norris, Joseph [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 27, 2002 12:53 PM
To: Perl Win32 Users (E-mail)
Subject: Active Directory question


I have the script below and so far it is working accept for one thing.
What I really need to get at is the Phone number. Can you tell me if I
am going about this the wrong way? I hacked some stuff from the Win32
Perl Scripting book (BTW - excellent book). So if there is anything I
need to clean up please let me know.  In addition I can not find any
source of 
how to update the Active directory with Perl.

Thanks.

script:

use Win32;
use Win32::OLE qw( in );
use Win32::OLE::Const 'Active DS Type Library';

my %Config;
my $user_name = $ARGV[0];
$Config{path} = "WinNT://" . Win32::DomainName();
if( $AD = GetADSIObject( \%Config ) )
{
  my $iCount = 0;
  my $Schema = Win32::OLE->GetObject( $AD->{Schema} );
  if( $Schema->{Container} ) {
    $AD->{Filter} = "f=user";
        my $Object;
    foreach $Object ( in( $AD ) ) {
                next if ($Object->{Class} ne 'User');
                ++$iCount if ($Object->{Name} eq $user_name);
                if ($iCount){
                        my $User = $AD->GetObject("", $Object->{Name});
                        print "name: $User->{Name}\n";
                        print "Phone: $User->{TelephoneNumber}\n";
                my $PropertyList = GetProperties( $User );
                foreach my $Property ( sort( @{$PropertyList} ) ){
                        next if( "" eq $Property );
                        print "\t $Property: $Object->{$Property}\n";
                }
                        last;
                }

    }
        if (!$iCount){
                print "user - $user_name not found\n";
        } else {
                print "found - $user_name\n";
        }
  } else {
    print "The '$AD->{ADsPath}' is a $AD->{Class} object.\n";
  }
}

sub GetADSIObject {
  my( $Config ) = @_;
  my $ADSIObject;
  my $ADsPath = $Config->{path};
  $ADSIObject = Win32::OLE->GetObject( $ADsPath );
  return( $ADSIObject );
}


sub GetProperties
{
  my( $Obj ) = @_;
  my @Properties;
  
  if( my $Schema = Win32::OLE->GetObject( $Obj->{Schema} ) )
  {
    foreach my $PropList ( $Schema->{MandatoryProperties},
                           $Schema->{OptionalProperties} )
    {
      push( @Properties, @{$PropList} ) if( defined $PropList );
    }
  }
  return( \@Properties );
}
_______________________________________________
Perl-Win32-Users mailing list [EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Win32-Admin mailing list [EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to