Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Find orphaned home directories

by OzzyOsbourne (Chaplain)
on Jun 08, 2001 at 00:38 UTC ( #86741=sourcecode: print w/replies, xml ) Need Help??
Category: NT Admin
Author/Contact Info OzzyOsbourne

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 to find orphaned home dir

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

my (@accounts,$x,$homeDir,$account);
my $PDC='yourPDC';
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 $in='accounts.txt';
my $out='orphans.txt';

# Slurp all accounts into @accounts...
print "Got accounts\n";
open (IN,"<$in");
open (OUT,">$out");
# Create a hash with user accounts and home directory paths
while (<IN>){
    ($account, $homeDir) = split /\t/; 
    $homeDir = lc ($homeDir);
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$
    if (!(-e "$dir1")){#if directory doesn't exist try d$
        if (!(-e "$dir1")){
# 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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://86741]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2022-10-04 22:38 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (19 votes). Check out past polls.