Leser: 4
6 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
use Win32::OLE;
my $sysinfo = Win32::OLE->new('ADSystemInfo') || die ("Can't get sysinfo: ".Win32::OLE->LastError()."\n");
my $usr=$sysinfo->{UserName};
my $AdsPath = "LDAP://".$usr;
my $adsuser = Win32::OLE->GetObject($AdsPath) || die ("Can't find user: ".Win32::OLE->LastError()."\n");
print "UserID: $adsuser->{samAccountName}\n";
print "Name: $adsuser->{displayName}\n";
print "Telefon: $adsuser->{telephoneNumber}\n";
print "Email: $adsuser->{EmailAddress}\n";
print "Office: $adsuser->{physicalDeliveryOfficeName}\n";
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use strict;
use Net::LDAP;
use Data::Dumper;
###############################################
# Account to bind to LDAP (Active Directory) #
# non-priviledged (could even be a guest account)
###############################################
my $account = 'cn=someuser, ou=someou, DC=my, DC=ad_domain, DC=com';
my $pwd = 'secret';
my $userid ='userid'; # Search User-ID
my %all_users;
############################################
# Bind to LDAP and query for DHCP servers #
############################################
my $ldap = Net::LDAP->new( 'mydc.ad_domain.com' ) or die "$@";
my $mesg = $ldap->bind( $account,
password => $pwd
);
$mesg = $ldap->search( # perform a search
base => "ou=someou, DC=my, DC=ad_domain, DC=com",
filter => "(&(objectclass=user)(samAccountname=$userid))"
);
$mesg->code && die $mesg->error;
##################################################
# retrieve the properties from the results #
##################################################
my $max = $mesg->count;
for ( my $i = 0; $i < $max; $i++ ) {
my $entry = $mesg->entry ( $i );
my $userid = $entry->get_value('samAccountname');
$all_users{$userid}{'first_name'} = $entry->get_value( 'givenName' );
$all_users{$userid}{'last_name'} = $entry->get_value( 'sn' );
$all_users{$userid}{'phone'} = $entry->get_value( 'telephoneNumber' );
}
################################
# unbind from LDAP #
################################
$mesg = $ldap->unbind;
print Dumper(\%all_users);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use Win32::OLE;
$Win32::OLE::Warn = 3;
my @attrs = qw(ADsPath cn sn givenName samAccountName displayName telephoneNumber
mail physicalDeliveryOfficeName facsimileTelephoneNumber);
my $searchString = "GC://dc=myDomain,dc=de;(&(objectClass=user)(sn=Mayer));" .
join(",", @attrs) . ";SubTree";
my $conn = Win32::OLE->new("ADODB.Connection");
$conn->{Provider} = "ADsDSOObject";
$conn->Open("ADSI Provider");
if (my $error = Win32::OLE->LastError) { die "Error in connect: $error\n"; }
my $adoCmd = Win32::OLE->new('ADODB.Command');
$adoCmd->{ActiveConnection} = $conn;
$adoCmd->{CommandText} = $searchString;
$adoCmd->Properties->{'Page Size'} = 50;
my $rs = Win32::OLE->new('ADODB.RecordSet');
$rs->Open( {Source => $adoCmd} );
while (not $rs->EOF()) {
my %object = map {
$attr[$_] => $rs->Fields($_)->{Value}
} 0..$#attr;
# mach was mit %object, z.B.
foreach my $attr (sort keys %object) { print "$attr => $object{$attr}\n" }
$rs->MoveNext;
} # while
$conn->Close();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use strict;
use Net::LDAP;
use Data::Dumper;
my %all_users;
############################################
# Bind to LDAP #
############################################
my $ldap = Net::LDAP->new( 'server01.hallo.de' ) or die "$@";
my $mesg = $ldap->bind( 'dc=hallo, dc=de',
password => 'user1'
);
$mesg = $ldap->search( # perform a search
base => "DC=hallo, DC=de",
filter => "(&(objectclass=user)(samAccountname=user1))"
);
$mesg->code && die $mesg->error;
##################################################
# retrieve the properties from the results #
##################################################
my $max = $mesg->count;
for ( my $i = 0; $i < $max; $i++ ) {
my $entry = $mesg->entry ( $i );
my $userid = $entry->get_value('samAccountname');
$all_users{$userid}{'first_name'} = $entry->get_value( 'givenName' );
$all_users{$userid}{'last_name'} = $entry->get_value( 'sn' );
$all_users{$userid}{'phone'} = $entry->get_value( 'telephoneNumber' );
}
################################
# unbind from LDAP #
################################
$mesg = $ldap->unbind;
print Dumper(\%all_users);
$mesg->code && die $mesg->error;
6 Einträge, 1 Seite |