use Win32; use Win32::AdminMisc; use Win32::NetAdmin; use strict; my $LogFile = "UnlockAccounts"; &Log ($LogFile, "Process Started."); my @Domains = ("AccountDomainName"); my @Accounts = ("UserAccount"); while (1) { my $sleeptime = 60; print "Sleeping [$sleeptime]"; print chr(13); while ($sleeptime > 0) { sleep 1; $sleeptime--; print "Sleeping [$sleeptime] "; print chr(13) } &UnlockAccounts(\@Domains, \@Accounts); } sub UnlockAccounts { my ($Domains_ref, $Accounts_ref) = @_; foreach my $Domain (@$Domains_ref) { # Repeated calls to GetPDC seems to cause the function to fail. my $PDC = "\\\\PDCname"; #my $PDC = Win32::AdminMisc::GetPDC($Domain); #if (! $PDC) { # &Log ($LogFile, "Domain:$Domain Failed to resolve PDC"); # next; #} foreach my $Account (@$Accounts_ref) { #Check to see if account exists and get current state. if (Win32::NetAdmin::UsersExist($PDC, $Account)) { my %Attribs; if (Win32::AdminMisc::UserGetMiscAttributes($PDC, $Account, \%Attribs)) { if ($Attribs{USER_FLAGS} & UF_LOCKOUT) { # See if the account is Locked Out &Log ($LogFile, "$Domain\\$Account is Locked"); my $Flags = $Attribs{USER_FLAGS} & ~UF_LOCKOUT; if (Win32::AdminMisc::UserSetMiscAttributes($PDC, $Account, USER_FLAGS=>$Flags)) { &Log ($LogFile, "$Domain\\$Account Unlocked"); } else { &Log ($LogFile, "$Domain\\$Account Failed to Unlock account"); } } } else { &Log ($LogFile, "$Domain\\$Account Get Attribs Failed"); } } else { &Log ($LogFile, "$Domain\\$Account Could not find account on PDC:$PDC"); } } } } exit; sub Log { my $file = shift; my $text = shift; # my $logpath = "c:\\PerlAutomation\\unlockaccount"; my $logpath = "c:\\data\\scripts"; my $time = localtime; open (LOG, ">>$logpath\\$file.log" ) || die "Could not open $file!\n"; print LOG "$time - $text\n"; close LOG; }