# chUsersProfile.pl changes assigned profile for domain users. # Written 14-sep-2000 by fgb - adapted from ListDisabledUsers.pl # # Replaced NetAdmin calls with AdminMisc (a more robust module) # which means AdminMisc must be installed on this workstation: # perl ppm.pl install http://www.roth.net/perl/packages/win32-adminmisc.ppd # # Mod 15-sep-2000, fgb: change login script too, what the hell. # Mod 05-feb-2002, fgb: updated for new times; *** DO N0T USE UNTIL AFTER MARDI-GRAS! ** require "ctime.pl"; use Win32::AdminMisc; $bDebug = 0; $bDoProfile = 1; $bDoScript = 0; MAIN: my $sAppName = "chUsersProfile"; my $sDataFile = ''; my $sDate = &ctime(time); my $nChangedProfiles = 0; my $nChangedScripts = 0; my $bInArray = 0; my $sPDC = "\\\\xyzsrv"; ### hardcoded PDC server name my $sNewProfile = "orange.man"; my $sNewScript = "login24.cmd"; my $sOldProfile = "grape.man"; my $sOldScript = "login6.cmd"; my $sProfile; my $sScript; $sTmp = ""; $sToday = substr($sDate,4); $sRptFile = "ChangedUsers.lis"; $sTempFile = "t.t"; $nTotal = 0; $sUser = ""; @userArray = ""; $bUseRptFile = 1; $sVersion = "1.2"; &ParseCmdArgs(); print "$sAppName $sVersion\n"; print "Change users profile from $sOldProfile to $sNewProfile\n"; if (length($sDataFile) < 3) { print "Getting list of domain users from DC $sPDC...\n"; Win32::AdminMisc::GetUsers($sPDC,'', \@userArray); $nTotal = @userArray; print "Retrieved $nTotal users.\n"; if ($nTotal > 0) { $bInArray = 1; } else { print "No users found on server $sPDC!\n"; exit; } goto _skipDataJunk; } else { open(FDATA,$sDataFile) || die "Can't open file $sDataFile: $!\n"; } _skipDataJunk: if ($bUseRptFile) { $sTmp = sprintf(">%s",$sRptFile); if ($bDebug) { print "sTmp = $sTmp\n"; } } open(RPTFILE,$sTmp) || die "Can't open file $sRptFile: $!\n"; print RPTFILE "$sAppName $sVersion report generated at $sToday\n\n"; if (! $bInArray) { while () { # read users in file: if (!/^\!/) { # skip comments chop $_; $sUser = $_; print "\nProcessing user $sUser..."; ++$nTotal; if ($bDoProfile) { &ProcessProfile(); } # if ($bDoScript) { &ProcessScript(); } # moved into ProcessProfile() } } close(FDATA); } else { # read usernames from array foreach $sUser (@userArray) { $_ = $sUser; if (/\$$/) { next; } # skip names ending in '$' print "\nProcessing user $sUser..."; ++$nTotal; if ($bDoProfile) { &ProcessProfile(); } # if ($bDoScript) { &ProcessScript(); } # moved into ProcessProfile() } } # format summary statement, print it and quit: $sTmp = "\n$sAppName: changed $nChangedProfiles profile"; if ($nChangedProfiles != 1) { $sTmp .= "s"; } if ($bDoScript) { $sTmp .= " and $nChangedScripts script"; if ($nChangedScripts != 1) { $sTmp .= "s"; } } $sTmp .= " (for $nTotal user"; if ($nTotal != 1) { $sTmp .= "s"; } $sTmp .= ").\n"; print RPTFILE $sTmp; close(RPTFILE); print $sTmp; # submarines: sub Help { printf("%s version %s\n",$sAppName,$sVersion); printf("\nUsage: %s [userlistfile] [/doscript] [/output=myfile.lis]\n",$sAppName); printf("\n/doscript changes login script in addition to profile.\n"); printf("If /output not specified, writes to $sRptFile\n"); } sub ParseCmdArgs { while (@ARGV) { $_ = shift @ARGV; if ($bDebug) { print "dbg: parsing $_\n"; } if (/^[\/\-]/) { # is arg a switch? $arg = lc(substr($_,1,3)); # parse 1st 3 chars: if ($arg eq "dos") { $bDoScript = 1; } if ($arg eq "out") { $i = index($_,"="); $i++; $sRptFile = sprintf("%s",substr($_,$i)); } if (($arg eq "hel") || (substr($arg,0,1) eq "?")) { &Help(); exit; } } else { # not a switch $sDataFile = $_; } } if ($bDebug) { print "dbg: sRptFile = $sRptFile\n"; print "dbg: sDataFile = $sDataFile\n"; } } sub ProcessProfile { Win32::AdminMisc::UserGetMiscAttributes('',$_,\%hash); $sProfile = $hash{USER_PROFILE}; $sScript = $hash{USER_SCRIPT_PATH}; if ($bDebug) { print "$_ profile=$sProfile script=$sScript\n"; } if ($sProfile =~ /$sOldProfile/) { $sProfile =~ s/$sOldProfile/$sNewProfile/; if ($bDebug) { print "dbg: changing profile to $sProfile\n"; } else { if (!Win32::AdminMisc::UserSetMiscAttributes('',$_,USER_PROFILE=>$sProfile)) { print "UserSetMiscAttributes failed\n"; } else { print "+"; } } print RPTFILE "$sUser profile changed\n"; ++$nChangedProfiles; } if ($bDoScript) { &ProcessScript(); } } sub ProcessScript { if ($sScript =~ /$sOldScript/) { $sScript =~ s/$sOldScript/$sNewScript/; if ($bDebug) { print "dbg: changing login script to $sScript\n"; } else { print "changing login script for $_ to $sScript\n"; if (!Win32::AdminMisc::UserSetMiscAttributes('',$_,USER_SCRIPT_PATH=>$sScript)) { print "UserSetMiscAttributes failed\n"; } } print RPTFILE "$sUser login script changed\n"; ++$nChangedScripts; } } __END__