Enable or disable user (Perl)

This code can be found in Chapter 6 of Active Directory Cookbook, 2nd edition

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# This Perl code will enable or disable a user.

# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
#      "Active Directory Cookbook" by Robbie Allen
# ISBN: 0-596-00466-4
# ---------------------------------------------------------------

# ------ SCRIPT CONFIGURATION ------
# Set to 1 to disable account or 0 to enable account
my $strDisableAccount = 0; 
my $strUserDN = "<UserDN>"; # e.g. cn=jsmith,cn=Users,dc=rallencorp,dc=com
# ------ END CONFIGURATION ---------
use Win32::OLE;
$Win32::OLE::Warn = 3;
my $objUser = Win32::OLE->GetObject("LDAP://" . $strUserDN);
if ($objUser->AccountDisabled == TRUE) {
   print "Account for ", $objUser->Get("cn"), " currently disabled\n";
   if ($strDisableAccount == FALSE) {
      $objUser->{AccountDisabled} = $strDisableAccount;
      $objUser->SetInfo;
      print "Account enabled\n";
   }
}
else {
   print "Account currently enabled\n";
   if ($strDisableAccount == 1) {
      $objUser->{AccountDisabled} = $strDisableAccount;
      $objUser->SetInfo;
      print "Account disabled\n";
   }
}

This code has been viewed 2020 times.

New from the creators of TechTasks.com: StatSheet.com