# From the book "Active Directory, Third Edition"
# ISBN: 0-596-10173-2
use Win32::OLE;
use strict;
use constant ForReading => 1;
my ($objDomain, $objUser, $fso, $tsInputFile, $strLine, $arrInput);
my ($fldUserHomedir, $wshShell);
$objDomain = Win32::OLE->GetObject('LDAP://cn=Users,dc=mycorp,dc=com');
$fso = Win32::OLE->new('Scripting.FileSystemObject');
# **********************************************************************
# Open the text file as a text stream for reading.
# Don't create a file if users-to-create.txt doesn't exist
# **********************************************************************
$tsInputFile = $fso->OpenTextFile('c:\\users-to-create.txt', ForReading, 0);
# **********************************************************************
# Execute the lines inside the loop, as long as you're not at the end
# of the file
# **********************************************************************
while (!$tsInputFile->AtEndOfStream) {
# **********************************************************************
# Read a line, and use the Split function to split the data set into
# its separate parts
# **********************************************************************
$strLine = $tsInputFile->ReadLine;
$arrInput = VBS::Split($strLine, ':');
$objUser = $objDomain->Create('user', 'cn=' . arrInput(0));
$objUser->Put('sAMAccountName', arrInput(0));
$objUser->Put('userPrincipalName', arrInput(0) . '@mycorp.com');
# **********************************************************************
# Write the newly created object out from the property cache
# Read all the properties for the object, including
# the ones set by the system on creation
# **********************************************************************
$objUser->SetInfo();
$objUser->GetInfo();
# **********************************************************************
# Set the password
# **********************************************************************
$objUser->SetPassword(arrInput(4));
# **********************************************************************
# Set the properties
# **********************************************************************
$objUser->{AccountDisabled} = 0;
$objUser->{AccountExpirationDate} = arrInput(1);
$objUser->{Description} = arrInput(2);
$objUser->{LoginScript} = '\\\\MYDOMAIN\\DFS\\Loginscripts\\' . arrInput(0) . '.vbs';
$objUser->{Profile} = '\\\\MYDOMAIN\\DFS\\Users\\' . arrInput(0) . '\\profile';
$objUser->{PasswordRequired} = 1;
$objUser->{DisplayName} = arrInput(3);
# **********************************************************************
# Set the drive that you'll map to
# **********************************************************************
$objUser->{HomeDirectory} = '\\\\MYDOMAIN\\DFS\\Users\\' . arrInput(0);
$objUser->Put('homeDrive', 'Z:');
$objUser->SetInfo();
# **********************************************************************
# Create the home directory
# **********************************************************************
if (!$fso->FolderExists('\\\\MYDOMAIN\\DFS\\Users\\' . arrInput(0))) {
$fldUserHomedir = $fso->CreateFolder('\\\\MYDOMAIN\\DFS\\Users\\' . arrInput(0));
}
# **********************************************************************
# Set full rights for the user to the home directory
# **********************************************************************
$wshShell = Win32::OLE->new('Wscript.Shell');
$wshShell->Run('cacls \\\\MYDOMAIN\\DFS\\Users\\' . arrInput(0) . ' /e /g ' . arrInput(0) . ':F', 1, 1);
# **********************************************************************
# Stop referencing this user
# **********************************************************************
$objUser = undef;
}
# Close the file
$tsInputFile->Close();
package VBS;
use strict;
sub Split {
my($expression, $delim, $count, $compare) = @_;
my $re;
$delim =~ s/\r//g;
if (!defined $delim || !length($delim)) {
$re = qr/\s/;
}
else {
$re = $compare ? qr/\Q$delim\E/i : qr/\Q$delim\E/;
}
if (!defined $count || $count == -1) {
return [CORE::split($re, $expression)];
}
else {
return [CORE::split($re, $expression, $count)];
}
}
|