Create mailbox (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 adds a mailbox in the first MDB on the server to an 
# existing user object

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

use Win32::OLE;
use Win32::OLE::Variant;

$strDCName = '<DC>';
#  e.g. "batman"
$strUserName = 'CN=<userCN>';
# e.g. "Random User"
# ------ END CONFIGURATION ------

# get the default and config NC names
$oIADS = Win32::OLE->GetObject('LDAP://RootDSE');
$strDefaultNC = $oIADS->Get('defaultnamingcontext');
$strConfigNC = $oIADS->Get('configurationNamingContext');
$strContainer = '/CN=Users,' . $strDefaultNC;
$objContainer = Win32::OLE->GetObject('LDAP://' . $strDCName . $strContainer);

# find the target user
$oIADSUser = Win32::OLE->GetObject('LDAP://' . $strUserName . ',CN=Users,' . $strDefaultNC);
$oMailBox = $oIADSUser;

# Open the Connection.
$oConnection = Win32::OLE->new('ADODB.Connection');
$oCommand = Win32::OLE->new('ADODB.Command');
$oRecordSet = Win32::OLE->new('ADODB.Recordset');
$oConnection->{Provider} = 'ADsDSOObject';
$oConnection->Open('ADs Provider');

# Build the query to find the private MDBs. Use the first one if any are found.
$strQuery = '<LDAP://' . $strConfigNC . '>;(objectCategory=msExchPrivateMDB);name,adspath;subtree';
$oCommand->{ActiveConnection} = $oConnection->{ConnectionString};
$oCommand->{CommandText} = $strQuery;
$oRecordSet = $oCommand->Execute;
if (!oRecordSet->EOF) {

    oRecordSet->MoveFirst();
    $firstMDB = Variant(VT_BSTR, 
    oRecordSet->Fields('ADsPath')->Value);
}
else {
    $firstMDB = '';
}

# create the mailbox
$oMailBox->CreateMailbox($firstMDB);
$oIADSUser->SetInfo();

print "Created mailbox for $strUserName\n";

This code has been viewed 1585 times.

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