#!win32-perl use strict; $|++; use Win32::AdminMisc; use Text::ParseWords; use Win32::NetAdmin qw(UserCreate LocalGroupAddUsers LocalGroupIsMember GroupIsMember); use Win32::Perms; use Win32::OLE; # Add Modules: # perl ppm.pl install http://www.roth.net/perl/packages/win32-perms.ppd # perl ppm.pl install http://www.roth.net/perl/packages/win32-adminmisc.ppd # CSV should be: Logon_Name, Full_Name, Primary_Group, Password # # Version: 1.3a # $server is location of Home Dirs my $server = "\\\\DataStore"; my $Dir; my ($logon, $name, $group, $pw, $year); my $flags = "UF_DONT_EXPIRE_PASSWD"; my $ldapdc = "DC=nmh, DC=nmhschool, DC=ORG"; my $validgroups = 'Teacher|Admin|Student'; # Open the CSV file of account info (format listed above) open IN, 'Add-Users.txt' or die "Could not open data file\n"; while () { # Check input, escape single quotes, ingore comments, etc next unless ($_); next if (/^$|^#/); s/'/\\'/g; # Parse the line and remove new lines ($logon, $name, $group, $pw) = "ewords(',', 0, $_); chomp ($logon, $name, $group, $pw); # Sanity checks and some parsing... die "Need valid group\n" unless ($group =~ /$validgroups/i); # Find class year of student from the logon name if ($group =~ /Student/i) { $year = substr($logon, -2); die "Not a valid year for student: $name\n" unless ($year =~ /\d{2}/); } # %OU is for the final OU placement my %OU = ("admin" => "Admin", "teacher" => "Teachers", "student" => "Students"); # %comment is for the comment field on the user form my %comment = ("admin" => "Staff", "teacher" => "Faculty", "student" => "Class of 20$year"); # %lgroup is the primary Local Group for the user my %lgroup = ("admin" => "Admin", "teacher" => "Teacher", "student" => "Students"); # %homes is the path for the user's Home Directory or Portfolio Directory my %homes = ("admin" => "AdminHome", "teacher" => "TeacherHome", "student" => "Students20$year", "portfolio" => "Portfolios\\Class20$year"); # For on-screen status of creation progress... print "Comment: " . $comment{lc($group)} . "\n"; # Create the user if it is not a member of "Domain Users" (meaning it does not exist) unless ( GroupIsMember('ProdDC-NF', "Domain Users", $logon) ) { UserCreate("ProdDC-NF", $logon, "$pw", 0, USER_PRIV_USER, '', "$comment{lc($group)}", UF_DONT_EXPIRE_PASSWD, 'default.bat') || print "Did not create user $logon\n"; print "Created User $logon\n"; } # Add the user to their primary local group unless ( LocalGroupIsMember('ProdDC-NF', "$lgroup{lc($group)}", "$logon") ) { LocalGroupAddUsers('ProdDC-NF', "$lgroup{lc($group)}", "$logon") || print "Could not add $logon to $lgroup{lc($group)}\n"; } # If it's a student, add them to their class year group if ($group =~ /Student/i) { LocalGroupAddUsers('ProdDC-NF', "Users20$year", "$logon") || print "Could not add $logon to Users20$year\n"; } # Set the Full name of the new user Win32::AdminMisc::UserSetMiscAttributes( '', $logon, USER_FULL_NAME => "$name") || print "Could not edit $logon\n"; # If the Home dir does not exist, create and permission it # SetOwner is a program from a product called Quota Advisor to change ownership of files and folders from the command line unless ( -d "$server\\$homes{lc($group)}\\$logon" ) { mkdir "$server\\$homes{lc($group)}\\$logon"; `cacls "$server\\$homes{lc($group)}\\$logon\" /E /G NMH\\$logon:F`; system("setowner /f $server\\$homes{lc($group)}\\$logon /o $logon"); } # If in the Students group, create and permission the Portfolio if ($group =~ /Student/i) { unless ( -d "$server\\d\$\\$homes{portfolio}\\$logon" ) { mkdir "$server\\d\$\\$homes{portfolio}\\$logon"; `cacls "$server\\d\$\\$homes{portfolio}\\$logon\" /E /G NMH\\$logon:F`; system("setowner /f $server\\d\$\\$homes{portfolio}\\$logon /o $logon"); } } # Use the 3rd party app TSCMD to populate the info on the TS tab # http://systemtools.com/free_frame.htm system("tscmd \\\\ProdDC-NF \"$logon\" TerminalServerProfilePath \"\\\\CitrixDS\\TSProfiles\$\\$logon\""); system("tscmd \\\\ProdDC-NF \"$logon\" TerminalServerHomeDir \"\\\\DataStore\\$homes{lc($group)}\\$logon\""); system("tscmd \\\\ProdDC-NF \"$logon\" TerminalServerHomeDirDrive \"H:\""); # Move user from the Users OU to the OU specified by the Group my $oContainer = Win32::OLE->GetObject("LDAP://OU=$OU{lc($group)}, $ldapdc"); my $oUser = $oContainer->MoveHere("LDAP://CN=$logon, cn=Users, $ldapdc","CN=$logon"); $oUser->SetInfo(); } close IN;