http://qs321.pair.com?node_id=86741
Category: NT Admin
Author/Contact Info OzzyOsbourne
Description:

People's accounts are deleted when they leave your company, but are their home directories? Hopefully yes, but sometimes, no. This will allow you to be sure.

There are 2 scripts, because in my domain, getting all of the account information tends to take a very long time.

The 1st script pulls all of the usernames and home dirs to a text file called accounts.txt. The second reads the user directories from your servers and checks to see if they are being used as home directories by comparing them to the accounts.txt. If the home dir is not being used by an active account, it is considered "Orphaned" and will be printed to orphan.txt. Then, you can flag it for deletion.

# Gets a list of users from the PDC and prints them to accounts.txt 
# This is used in conjunction with orphan.pl to find orphaned home dir
+ectories.

use strict;
use Win32::NetAdmin qw(GetUsers UserGetAttributes);

my (@accounts,$x,$homeDir,$account);
my $PDC='yourPDC';
my $filter='FILTER_NORMAL_ACCOUNT';
my $out='accounts.txt';

# Slurp all accounts into @accounts...
GetUsers($PDC,$filter, \@accounts) or die "Can't get users $!";
print "got accounts\n";
open OUT,">$out";

foreach $account(@accounts){
    UserGetAttributes($PDC,$account,$x,$x,$x,$homeDir,$x,$x,$x) or war
+n "UserGetAttributes() failed: $^E";
    $homeDir=lc ($homeDir);
    print OUT "$account\t$homeDir\n";
        
}

close OUT;
End of script 1 Start of script 2
# Compares directories on the servers against who is using them in use
+r manager 
# If no one claims the directory in user manager, it is dubbed an orph
+an and 
# printed to orphans.txt

use strict;
use Win32::NetAdmin qw(GetUsers UserGetAttributes);

my (@accounts,%is_acct,$x,$key,$homeDir,$account,$userDir,$server);
my $PDC='yourPDC';
my @servers=('yourserver1','yourserver2');
my $filter='FILTER_NORMAL_ACCOUNT';
my $in='accounts.txt';
my $out='orphans.txt';

# Slurp all accounts into @accounts...
print "Got accounts\n";
%is_acct=();
open (IN,"<$in");
open (OUT,">$out");
# Create a hash with user accounts and home directory paths
while (<IN>){
    chomp;
    s/\\/\//g;
    ($account, $homeDir) = split /\t/; 
    $homeDir = lc ($homeDir);
    $is_acct{$homeDir}=$account;
}
print "Hash complete\n";

# Check for user shares on D$ and E$ and exit if not there...
foreach $server (@servers){
    my $dir1="//$server/d\$/users";
    if (!(-e "$dir1")){#if directory doesn't exist try d$
        $dir1="//$server/e\$/users";
    if (!(-e "$dir1")){#if directory doesn't exist try d$
            $dir1="//$server/users";
        if (!(-e "$dir1")){
            next;
        }
        }
    }
# Read in the user shares from the servers 
    opendir(DIR, $dir1) or die "can't opendir $dir1: $!";
    my @dirs = grep { !/^\./ && -d "$dir1/$_" } readdir(DIR) or warn "
+can't grep"; #weed out dots and get only dirs
    closedir DIR;
    @dirs = map (lc "//$server/$_", @dirs);
    print "read directories from $server\n";

    foreach $userDir (@dirs){
       print OUT "$userDir";
        if(!exists $is_acct{$userDir}){
            print OUT ": Orphan";
        }
    print OUT "\n";
    }
}
close OUT;
End of script 2