#!win32-perl use strict; $|++; use Win32::AdminMisc; use Text::ParseWords; use Win32::NetAdmin qw(UserCreate LocalGroupAddUsers LocalGroupIsMembe +r GroupIsMember); use Win32::Perms; use Win32::OLE; # Add Modules: # perl ppm.pl install http://www.roth.net/perl/packages/win32-perms.pp +d # perl ppm.pl install http://www.roth.net/perl/packages/win32-adminmis +c.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 (<IN>) { # 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) = &quotewords(',', 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 Dir +ectory 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)}", "$l +ogon") ) { 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 chan +ge 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 $logo +n"); } # 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, $lda +pdc","CN=$logon"); $oUser->SetInfo(); } close IN;

In reply to AD Account Creation by jds

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.