Create user and 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 creates a new user mailbox, then mail-enables
# it by creating a mailbox in the first MDB on the server.
# ------ SCRIPT CONFIGURATION ------
use Win32::OLE;
use Win32::OLE::Variant;
$strDCName = '<serverName>';
# e.g. "<DC>"
$strUserName = '<user>';
# e.g. "jrandomuser"
$strFirstName = 'Joe';
$strLastName = 'Blow';
$strPassword = 'G0bbeldygook!#';
# ------ END CONFIGURATION ------
$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);
$NewUser = $objContainer->Create('User', 'cn=' . $strUserName);
$NewUser->{firstName} = $strFirstName;
$NewUser->{lastName} = $strLastName;
$NewUser->Put('sAMAccountName', $strUserName);
$NewUser->SetInfo();
$NewUser->{AccountDisabled} = 0;
$NewUser->SetPassword($strPassword);
$NewUser->SetInfo();
# Open the connection.
$theConnection = Win32::OLE->new('ADODB.Connection');
$theCommand = Win32::OLE->new('ADODB.Command');
$theRecordSet = Win32::OLE->new('ADODB.Recordset');
$theConnection->{Provider} = 'ADsDSOObject';
$theConnection->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';
$theCommand->{ActiveConnection} = $theConnection->{ConnectionString};
$theCommand->{CommandText} = $strQuery;
$theRecordSet = $theCommand->Execute;
if (!theRecordSet->EOF) {
theRecordSet->MoveFirst();
$firstMDB = Variant(VT_BSTR,
theRecordSet->Fields('ADsPath')->Value);
}
else {
$firstMDB = '';
}
# create the mailbox
$NewUser->CreateMailbox($firstMDB);
$NewUser->SetInfo();
print "Mailbox created successfully\n";
|
This code has been viewed 1293 times.
|
New from the creators of TechTasks.com:
StatSheet.com
|