Bulk add from excel (Perl)

This code can be found in Chapter 5 of Exchange Server Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# This script was originally published in the Exchange Cookbook,
# (http://www.exchangebookcook.com). Written by Paul Robichaux, 
# Missy Koslosky, and Devin Ganger. Redistributed with permission 
# of the publisher, O'Reilly & Associates.

# This code will read the contents of an Excel file and use the fields
# to create a set of mailboxes 

# ------ SCRIPT CONFIGURATION ------

use Win32::OLE;

$strExchangeServerName = '<serverName>';
# e.g. " batman"
$strWorksheetName = '<filePath>';
# e.g. "c:\data\excelSheet.xls"; replace with actual name  
$strTargetMDB = '<someMDB>';
$strTargetContainer = 'cn=users,<forestDN>';
# e.g. "cn=Users, dc=domain, dc=com"

# ------ END CONFIGURATION ---------

# create an invisible Excel instance and open our specified workbook
$objExcel = Win32::OLE->new('Excel.Application');
$objWorkbook = $objExcel->Workbooks->Open($strWorksheetName);

# assume that row 1 is header data, and that rows 2-N contain data
# Columns:
# 1: first name
# 2: last name
# 3: CN
# 4: alias

$currentRow = 2;
until ($objExcel->Cells($currentRow, 1)->Value eq '') {
    $userFirst = $objExcel->Cells($currentRow, 1)->Value;
    $userLast = $objExcel->Cells($currentRow, 2)->Value;
    $userCN = $objExcel->Cells($currentRow, 3)->Value;
    $userAlias = $objExcel->Cells($currentRow, 4)->Value;

    $objTargetContainer = Win32::OLE->GetObject($strTargetContainer);
    $objUser = $objOU->objTargetContainer('User', 'cn=' . $userCN);
    $objUser->{sAMAccountName} = $userAlias;
    $objUser->{GivenName} = $userFirst;
    $objUser->{SN} = $userLast;
    $objUser->{AccountDisabled} = 0;
    $objUser->SetInfo();
    $objUser->CreateMailbox($targetMDB);
    $currentRow = $currentRow + 1;
}
$objExcel->Quit();
print 'Imported ' . $currentRow - 1 . ' objects', "\n";

This code has been viewed 1163 times.

New from the creators of TechTasks.com: StatSheet.com