Программа, перемещающая объекты
Code That Moves Objects
use strict;
use Net::LDAP;
BEGIN COMMENT
# Customize for your environment.
# Настройте для вашего окружения
END COMMENT
my $dc = ?dc1?;
my $user = ?administrator@mycorp.com?;
my $passwd = ?Adminpasswd?;
# BEGIN CALLOUT A
my $old_parent = «cn=users, dc=mycorp, dc=com»;
my $new_parent = «ou=sales, dc=mycorp, dc=com»;
# END CALLOUT A
BEGIN COMMENT
# Connect and authenticate.
END COMMENT
my $ldap = Net::LDAP->new($dc) or die «$@
»;
my $rc = $ldap->bind( $user, password => $passwd);
die $rc->error if $rc->code;
# BEGIN CALLOUT B
BEGIN COMMENT
# Find all user objects whose department attribute is Sales.
# Поиск всех объектов user, чей атрибут department = Sales.
END COMMENT
my $search = $ldap->search (
base => $old_parent,
scope => ?one?,
filter => «(&(objectclass=user)(objectcategory=Person)
(department=Sales))»,
attrs => [?cn?],
);
die $search->error if $search->code;
# END CALLOUT B
# BEGIN CALLOUT C
BEGIN COMMENT
# Move matching users to the Sales OU.
# Перемещение соответствующих пользователей в Sales OU
END COMMENT
my $count = 0;
foreach my $entry ($search->entries) {
$rc = $ldap->modrdn($entry->dn,
newrdn => ?cn=?. $entry->get_value(?cn?),
newsuperior => $new_parent,
deleteoldrdn => 1 );
if ($rc->code) {
print «Move failed for «, $entry->get_value(?cn?), «: «, $rc->error, «
»;
}
else {
$count++;
print «Move successful: «, $entry->get_value(?cn?), «
»;
}
}
print «Total successfully moved: $count
»;
# END CALLOUT C
$ldap->unbind;