[JaxPM] HomeDrive Cleanup Script

Roy Sprague roy.sprague at phc.aetna.com
Tue Feb 1 09:28:08 CST 2000


On the jacksonville-pm-list; Jax.PM'er "Roy Sprague" <roy.sprague at phc.aetna.com> wrote -



I've been working on the following script to clean up data off of users
homeshares.  I would like to be able to
provide the user with a HTML page listing in a table to filename, size, and date
 last touched.  Also give the
user some suggestion for files to delete and some other general information
about their homeshare. The
HTML file will be placed into the homeshare and I'll use the SMTP module to send
 the user a note with
the anchor to that file.

All of which I can print to a text file but I think it would be better received
if it was something they could
read via a browser.  I've looked a CGI.pm and don't know if that would be the
best option to
generate the HTML file.

Does anyone have any suggestion?

Thanks, Roy

PS. This script uses the resource kit utility DIRUSE.exe to get the initial
size.  If anyone knows of a faster
way I'd like to see it.

# This script will check users home directories over for size and none use.
# Checks to see if the account is active.
# Places a file in the root of the users share containing information about file
 sizes and use.

use Win32::AdminMisc;
use Win32::File;
use File::stat;

(@ARGV) or &PrintHelp;

undef $output;

if ($ARGV[0]) {
  $path = $ARGV[0];

  if ($ARGV[1]) {
    open(OUTFILE, ">$ARGV[1]") or
    die "Unable to open file $ARGV[1] for output\n";
    $output = "OUTFILE";
  }
}
else {
  &PrintHelp;
}
@spin = ("\/","\-","\\","\|");
print "Getting list of user accounts from $path\n";
$accountDC = "\\\\Domainserver";
$currenttime = time;
$maxsize = 100;                    # Maximum directory size of 100M
$checktime = 90 * 60 * 60 * 24;          # 90 days.
$maxpasswordage = 90 * 60 * 60 * 24;     # 90 days.
$gttime = $currenttime - $checktime;
@basedir = `dir $path /b/o`;

foreach $username (@basedir) {
  chomp $username;
  $cnt++;
  print "\rChecking User Shares of $path $spin[$cnt++ % 4]      ";

  if (Win32::AdminMisc::UserGetMiscAttributes($accountDC, $username, \%usrhash))
 {   # Checking user account and skipping if disable
    if ($usrhash{USER_PASSWORD_AGE} > $maxpasswordage) {
      $op = "\r$username password is greater than 90 days old.  Skipping
$path\\$username    \n";
      &Print_Output();
      next;
    }

    unless ($usrhash{USER_FLAGS} & UF_ACCOUNTDISABLE ne 0) {
      $op = "\r$username account is disabled.  Skipping that directory
            \n";
      &Print_Output();
      next;
    }
    else {
      undef $userdir;
      $userdir = "$path\\$username";
      &DirSize();
    }
  }
  else {
    $op = "\r$username not found on $accountDC.  Skipping that directory
            \n";
    &Print_Output();
    next;
  }
  undef @windir;
}
close(OUTFILE);

sub DirSize {
  undef @view;

  @view = `diruse $userdir /M`;

  undef $size;
  undef $count;

  foreach $line (@view) {
    if (index($line," TOTAL") ge 0) {
      $line =~ s/ +([.0-9]+) +([0-9]+) +TOTAL: +(.+)/$2 $3 $1/;
      $size = $1;
      $count = $2;
      $userpath = $3;
    }
  }
  if ($size >= 100) {
    $op = "\r$size\t$count\t$username\t$usrhash{USER_FULL_NAME}\t\t    \n";
    &Print_Output();
    &Check_Userdata();
  }
}

sub Check_Userdata {
  $numgt90 = 0;
  $sizegt90 = 0;
  @windir = `dir $userdir /b /s /o`;

  foreach $filename (@windir) {
    $cnt++;
    print "\rChecking the $count files information $spin[$cnt++ % 4]";
    chomp $filename;
    Win32::File::GetAttributes($filename, $attrib);
    undef @filedates;
    unless ($attrib & DIRECTORY) {
      my (@t) = @{stat $filename};
      @filedates = (@filedates, $t[8]);
      @filedates = (@filedates, $t[9]);
      @filedates = (@filedates, $t[10]);
      @filedates = sort { $a <=> $b } @filedates;
      $file_check = @filedates[@filedates-1];
      if ($file_check lt $gttime) {
        $numgt90++;
        $sizegt90 += $t[7];
      }
    }
  }
  $op = "\r$numgt90 file $sizegt90 bytes not touched in 90 days out of
$count\n";
  &Print_Output();
}

sub Print_Output {
  print $op;
  if ($output) {
    print $output "$op";
  }
}

sub Check_syntax {
  (@ARGV) or &PrintHelp;

  if ($ARGV[0]) {
    $path = $ARGV[0];
    Win32::File::GetAttributes($path, $attrib);
    if ($attrib & DIRECTORY) {
      die "Unable to find directory $path\n";
    }
    if ($ARGV[1]) {
      open(OUTFILE, ">$ARGV[1]") or
      die "Unable to open file $ARGV[1] for output\n";
      $output = "OUTFILE";
    }
  }
  else {
    &PrintHelp;
  }
}

sub PrintHelp {
  print <<END;

Syntax:

usrdatclp <path> <optional output filename>

Path:  Path to be checked including subdirectories (UNC's allowed).

Output File: The output file for logging (Optional).

END
  exit;
}



Jax.PM Moderator's Note:
This message was posted to the Jacksonville Perl Monger's Group listserv.
The group manager can be reached at -- owner-jacksonville-pm-list at pm.org
to whom send all praises, complaints, or comments...




More information about the Jacksonville-pm mailing list