#!/usr/bin/perl
#
#
#Creation date: 10/4/2004
# This script will perform four major operations
# 1) Read an LDIF extract from Active Directory,
# objectClass=Users. Copy DN and samACCOUNTNAME into a hash.
# $key is dn and $value is samACCOUNTNAME
# 2) Read a CSV extract from a legacy AS400 Database into @hris.
# 3) Compare hash and @hris and match the samACCOUNTNAME
# value and and value [0] from @hris. On match and write record containg DN,
# samACCOUNTNAME from @mlf1 and Division and Location from
# @hris.
# 4) Write record to file an LDIF record using the DN, samACCOUNTNAME,
# division,and personalTitle attributes.
use warnings;
use strict;
open (my $FILE, 'c:/test/ldif.ldf') || die "can't open 'ldif.ldf': $!"; # open LDIF file for reading
open (my $OUTPUT_FILE, '>mlfexport.txt') || die "can't open 'mlfexport.txt': $!"; #open output file
open (my $FILE1, 'c:/test/hris.csv') || die "can't open 'hris.csv': $!"; #open csv file for reading
local $/ = "\n\n"; # Set an empty newline as line separator
my %lookup; #hash for $File
my($key, $value);
my $dn = $key; # want to assign the dn as the key in the hash table
my $samACCOUNTNAME = $value; #want to assign samACCOUNTNAME to value
#next block will hash $file for 2 values
while (<$FILE>) {
my ($dn)= /^dn: (.*)$/m;
my ($samACCOUNTNAME)= /^sAMAccountName: (.*)$/m;{
$lookup {$dn} = {$samACCOUNTNAME} # verify syntax, is $lookup defined right, and is this matching dn and samACCOUNTNAME
if defined $samACCOUNTNAME and $dn ; # only if both are found
}
}
# I think I am making this too complicated. Do I need all these variable declarations?
my @hris = ($FILE1); #associate csv file to array
my @ldif = (%lookup); #associate table to array
my $uid = ((@hris)[0]); #set usernamme which also equals samACCOUNTNAME
my $div = ((@hris)[1]); #set division value for printing
my $loc = ((@hris)[2]); #set personalTitle value for printing
# The next block should look for the samACCOUNTNAME in $value and match it to array position 0 in hris.
while (<$FILE1>) {
foreach my $value (keys %lookup){
if $hris [1] eq $lookup($value) # what I want to do here is validate a match on $value and array [0] and print the entry to the output file
}
&print_entry($dn, $uid, $div, $loc);
}
#this block prints the output
sub print_entry {
local($dn, $uid, $div, $loc) = @_; #print out LDIF file
print $OUTPUT_FILE "dn:$ldif[2]","\n";
print $OUTPUT_FILE "changetype:=modify","\n";
print $OUTPUT_FILE "sAMAccountName:=$uid","\n";
print $OUTPUT_FILE "division:=$hris[1]","\n";
print $OUTPUT_FILE "personalTitle:=$hris[2]","\n";
print $OUTPUT_FILE "\n";
}
close($OUTPUT_FILE) || die "can't close mlfexport.txt: $!";
close($FILE1) || die "can't close 'hris.csv': $!";
close($FILE) || die "can't close 'ldif.ldf': $!";
exit; # successful exit
####
C:\test>perl mlfimport.pl
syntax error at mlfimport.pl line 55, near "if $hris "
Global symbol "$lookup" requires explicit package name at mlfimport.pl line 55.
Execution of mlfimport.pl aborted due to compilation errors.
####
dn:CN=Patel\, Rupal,OU=Users,OU=BartorRd,OU=Corporate,DC=oakleaf,DC=ca
changetype:=modify
sAMAccountName:=HASH(0x224e0c)
division:=
personalTitle:=