Modify bit flag (Perl)

This code can be found in Chapter 4 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 safely modifies a bit flag attribute

# ---------------------------------------------------------------
# Adapted from VBScript code contained in the book:
#      "Active Directory Cookbook" by Robbie Allen
# Publisher: O'Reilly and Associates
# ISBN: 0-596-00466-4
# Book web site: http://rallenhome.com/books/adcookbook/code.html
# ---------------------------------------------------------------

# ------ SCRIPT CONFIGURATION ------
my $strObject = "<ObjectDN>";       # e.g. cn=jsmith,cn=users,dc=rallencorp,dc=com
my $strAttr = "<AttrName>";         # e.g. rallencorp-UserProperties
my $boolEnableBit = <TRUEorFALSE>;  # e.g. 0 or 1 
my $intBit = <BitValue>;            # e.g. 16
# ------ END CONFIGURATION ---------
use Win32::OLE;
$Win32::OLE::Warn = 3;
my $objObject = Win32::OLE->GetObject("LDAP://" . $strObject);
my $intBitsOrig = $objObject->Get($strAttr);
my $intBitsCalc = CalcBit($intBitsOrig, $intBit, $boolEnableBit);

if ($intBitsOrig != $intBitsCalc) {
   $objObject->Put($strAttr, $intBitsCalc);
   $objObject->SetInfo;
   print "Changed $strAttr from $intBitsOrig to $intBitsCalc\n";
}
else {
   print "Did not need to change $strAttr ($intBitsOrig)\n";
}


sub CalcBit {
my ($intValue, $intBit, $boolEnable) = @_;

   if ($boolEnable) {
      return $intValue | $intBit;
   }
   else {
      if ($intValue and $intBit) {
         return $intValue ^ $intBit;
      }
      else {
         return $intValue;
      }
   }
}

This code has been viewed 1420 times.

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