# loginRpt script lists all NT domain login scripts and the number # of users each is assigned to. # Written 9-apr-1999 by fhb. # Mod 1-jul-1999 by fhb: add script arg to list users running one script; # implemented report file feature. # Mod 1-mar-2000, fgb: added /noscript option to list users with no # assigned login script. # # *** NB: "last" command can't be used in while(($sName,$nCount) = each(%hScript)) # *** since it doesn't reset the each iteration! # # Of interest: CaseInsensitive sort subroutine; Win32 NetAdmin stuff. require "ctime.pl"; use Win32; use Win32::NetAdmin; $bDebug = 0; INIT: $sAppName = "loginRpt"; $sDate = &ctime(time); $bFound = 0; @aMatchingUsers; $bNoScript = 0; $sToday = substr($sDate,4); $sRptFile = ""; %hScript = (); @hScript{"login.bat","login-ff.cmd","login97.cmd"} = (0,0,0); $sScript = ""; $bSingleScript = 0; $sTempFile = "t.t"; $nTotal = 0; $sUser = ""; $bUseRptFile = 1; @aUsers; $sVersion = "1.2"; MAIN: print "$sAppName $sVersion\n"; &ParseCmdArgs(); Win32::NetAdmin::GetDomainController(Win32::NodeName(),Win32::DomainName(),$sDC); #print "DC: $sDC\n"; print "Getting domain users from $sDC\n"; Win32::NetAdmin::GroupGetMembers($sDC,"Domain Users",\@aUsers); $n = 0; $nUsers = @aUsers; if ($bNoScript) { $sScript = "[no script]"; } if ($bSingleScript) { print "Checking $nUsers users for $sScript..."; } foreach $u (@aUsers) { if (not $u =~ /\$+/) { # skip machine accounts if ($bDebug) { print "\ndbg> checking user $u\n"; } Win32::NetAdmin::UserGetAttributes($sDC,$u,$pwd,$pwag,$prv,$hom,$cmt,$f,$scrpt); if ($bSingleScript) { if ($scrpt eq $sScript) { push @aMatchingUsers,$u; } else { if ($bNoScript && ($scrpt eq "")) { push @aMatchingUsers,$u; } } } else { if ($scrpt eq "") { $scrpt = "[none]"; } $bFound = 0; while(($sName,$nCount) = each(%hScript)) { if ($sName eq $scrpt) { $bFound = 1; ++$hScript{$sName}; # inc count for match in hash ### last; } } if ($bFound == 0) { $hScript{$scrpt} = 1; } } } # end else (collect all script stats) ++$n; if (!($n % 10)) { print "."; } # print '.' for every 10 users } print "\n"; if ($sRptFile) { $sOut = ">$sRptFile"; open(RPTFIL,$sOut) || die "Can't open file $sRptFile: $!"; print RPTFIL "$sAppName report generated on $sToday\n"; } if (! $bSingleScript) { @sortedList = reverse sort byCount keys(%hScript); foreach (@sortedList) { print "$_: $hScript{$_}\n"; if ($sRptFile) { print RPTFIL "$_: $hScript{$_}\n"; } } } else { $nMatches = @aMatchingUsers; print "\nFound $nMatches users with script $sScript:\n"; if ($sRptFile) { print RPTFIL "Found $nMatches users with script $sScript:\n"; } @sortedList = sort CaseInsensitive @aMatchingUsers; foreach (@sortedList) { print "$_\n"; if ($sRptFile) { print RPTFIL "$_\n"; } } if (!$sRptFile) { print "\nComplete.\n"; } } if ($sRptFile) { close RPTFIL; print "\nCreated report file $sRptFile\n"; } ### subroutines: ### sub byCount { return $hScript{$a} <=> $hScript{$b}; } sub CaseInsensitive { lc($a) cmp lc($b); } sub Help { print "\n$sAppName version $sVersion\n"; print "\nusage: $sAppName [[scriptname] | [/noscript]] [/output=filespec]\n"; print "\n 'noscript' option lists accounts with no assigned login script\n"; } sub ParseCmdArgs { while (@ARGV) { $_ = shift @ARGV; if ($bDebug) { print "dbg: parsing $_\n"; } if (/^[\/\-]/) { # is arg a switch? $arg = substr($_,1,3); # parse 1st 3 chars: if (($arg eq "hel") || (substr($arg,0,1) eq "?")) { &Help(); exit; } if ($arg eq "out") { $i = index($_,"="); $i++; $sRptFile = sprintf("%s",substr($_,$i)); } if ($arg eq "nos") { # 'noscript' $bSingleScript = $bNoScript = 1; } } else { # not a switch $sScript = $_; $bSingleScript = 1; } } if ($bDebug) { print "dbg: sRptFile = $sRptFile\n"; } }