1    #!/usr/bin/perl -w
   2    
   3    # This Perl script is used to bulk-load food services users into Active Directory.
   4    # The program assumes the existence of the file food.csv, pre-populated with columns
   5    # in a specific ordering.
   6    
   7    # The Win32::OLE module contains Active Directory Scripting elements
   8    use Win32::OLE;
   9    
  10    ## Examples of Active Directory Scripting can be found here: 
  11    ## http://techtasks.com/code/viewbookcode/1556
  12    
  13    # This is the file of user information we'll read in; must be in the same directory
  14    my $FilePath = "food.csv";
  15    
  16    # Open up a read only file handle to the file to be imported
  17    open (IN,"$FilePath")|| die "The file was not created correctly: $!\n";
  18    
  19    # This is where we would define what OU (organizational unit) the user should be created in. 
  20    # This will be moved into the loop once we are ready to have the OU dynamically generated
  21    
  22    # Variable declarations
  23    my $discard;
  24    my $CommonName;
  25    my $samName;  
  26    my $UserPrincipalName;
  27    my $DepartmentName;
  28    my $EmployeeID;
  29    my $Title;
  30    my $ignore;
  31    my $Location_Code;
  32    my $Title_CD;
  33    my $FirstName;
  34    my $LastName;
  35    my $MiddleName;
  36    my $ObjOrganizationalUnit;
  37    my $objUser;
  38    my $ShortUserPrincipalName;
  39    my @namesplitter;
  40    my $counter=0;
  41    my $Phone;
  42    my $City;
  43    my $State;
  44    my $Office;
  45    my $Company;
  46    my $Country;
  47    my $Password = "System01";
  48    
  49    # Create a log file of accounts created, for reference
  50    open OUT, ">accounts.txt" or die $!;
  51    
  52    #Throw away the first line of the input file; it contains field names
  53    $discard = <IN>;
  54    
  55    # Step through each line of the input file
  56    while (<IN>){
  57            $counter=0;
  58            
  59    # Robins Format: Name,Description,Business Phone,City,Company,Country,Job Title,Department,
  60    #                Office,Exchange Alias,First Name,Last Name,State,User Logon Name
  61    
  62            ($CommonName,$EmployeeID,$Phone,$City,$Company,$Country,$Title,$DepartmentName,
  63             $Office,$ShortUserPrincipalName,$FirstName,$LastName,$State,$UserPrincipalName)= split(/,/, $_);
  64            
  65            chomp($UserPrincipalName);
  66            
  67            # Open the OU we'll use; it has to exist already, else its a fatal error
  68            my $strParentDN = "LDAP://ou=foodimport,dc=fresnounified,dc=org";
  69            $ObjOrganizationalUnit = Win32::OLE->GetObject($strParentDN) || die (Win32::OLE->LastError()."\n");
  70    
  71            # Now we'll create the user object, and afterwards, populate its attributes
  72            $objUser = $ObjOrganizationalUnit->Create("User", "cn=".$CommonName);
  73             
  74            # Fix up these three fields after reading them in
  75            $UserPrincipalName = lc($UserPrincipalName);
  76            $ShortUserPrincipalName = lc($ShortUserPrincipalName);
  77            $samName = $ShortUserPrincipalName;
  78            if ($Phone eq '' ) { $Phone = "559-000-0000"};
  79            if ($EmployeeID eq '' ) { $EmployeeID = "000000000"};
  80            $FirstName =~ s/ //g;
  81            $LastName =~ s/ //g;
  82            $FirstName = ucfirst(lc($FirstName));
  83            $LastName = ucfirst(lc($LastName));
  84            $CommonName = $FirstName." ".$LastName;
  85            
  86            # These print statements are purely for debug output; they don't do anything
  87            print "userPrincipalName      ".$UserPrincipalName."\n";
  88            print "    givenName          ".$FirstName."\n";
  89            print "    surname            ".$LastName."\n";
  90            print "    mail               ".$UserPrincipalName."\n";
  91            print "    ShortUserPN          ".$ShortUserPrincipalName."\n";
  92            print "    samName              ".$samName."\n";
  93            print "    displayName                ".$FirstName." ".$LastName."\n";
  94            print "    department                 ".$DepartmentName."\n";
  95            print "    company            ".$EmployeeID."\n";
  96            print "    Title              ".$Title."\n";
  97            print "    physDelivOfficeName        ".$DepartmentName."\n";
  98            print "    description                ".$EmployeeID."\n";
  99            print "    password           ".$Password."\n";
 100            
 101            # Set the sAMAccountName. Put() can be used for any attribute in the schema that we want to add.
 102            # To find the attribute name, see here: http://msdn2.microsoft.com/en-us/library/ms675090.aspx
 103            # you must use the 'LDAP display name' to set the attributes with this code.
 104            # This name is different than the formal attribute name.
 105            
 106            # e.g. extilto, 7-character handle
 107            $objUser->Put("sAMAccountName", $samName);
 108    
 109            # Set the User Principal Name (UPN)
 110            $objUser->Put("userPrincipalName", $UserPrincipalName);
 111            $objUser->SetInfo;
 112            
 113            # Populate the remaining fields
 114            $objUser->Put("c",$Country);
 115            $objUser->Put("givenName", $FirstName);
 116            $objUser->Put("sn", $LastName);
 117            $objUser->Put("mail", $UserPrincipalName);
 118            $objUser->Put("displayName", $FirstName ." ". $LastName);
 119            $objUser->Put("department", $DepartmentName);
 120            $objUser->Put("company", "Fresno Unified School District");
 121            $objUser->Put("Title", $Title);
 122            $objUser->Put("physicalDeliveryOfficeName", $DepartmentName);
 123            $objUser->Put("description", $EmployeeID);
 124            $objUser->Put("telephoneNumber",$Phone);
 125    
 126            # Give the user a password.  Must meet password complexity rules
 127            $objUser->SetPassword($Password);
 128            $objUser->{AccountDisabled} = FALSE;
 129    
 130            # Write everything to AD.
 131            $objUser->SetInfo();
 132            print OUT $UserPrincipalName .": ". $Password."\n";
 133    }
 134    
 135    
 136    
 137