View trusts (Perl)

This code can be found in Chapter 2 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 prints the trusts for the specified domain.

# ---------------------------------------------------------------
# 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 $strDomain = "<DomainDNSName>";   # e.g. rallencorp.com
# ------ END CONFIGURATION ---------

use Win32::OLE;
$Win32::OLE::Warn = 3;

# Trust Direction Constants taken from NTSecAPI.h
my %objTrustDirectionHash;
$objTrustDirectionHash{"DIRECTION_DISABLED"} = 0;
$objTrustDirectionHash{"DIRECTION_INBOUND"} =  1;
$objTrustDirectionHash{"DIRECTION_OUTBOUND"} = 2;
$objTrustDirectionHash{"DIRECTION_BIDIRECTIONAL"} = 3;

# Trust Type Constants - taken from NTSecAPI.h
my %objTrustTypeHash;
$objTrustTypeHash{"TYPE_DOWNLEVEL"} = 1;
$objTrustTypeHash{"TYPE_UPLEVEL"} = 2;
$objTrustTypeHash{"TYPE_MIT"} = 3;
$objTrustTypeHash{"TYPE_DCE"} = 4;

# Trust Attribute Constants - taken from NTSecAPI.h
my %objTrustAttrHash;
$objTrustAttrHash{"ATTRIBUTES_NON_TRANSITIVE"} = 1;
$objTrustAttrHash{"ATTRIBUTES_UPLEVEL_ONLY"} = 2;
$objTrustAttrHash{"ATTRIBUTES_QUARANTINED_DOMAIN"} = 4;
$objTrustAttrHash{"ATTRIBUTES_FOREST_TRANSITIVE"} = 8;
$objTrustAttrHash{"ATTRIBUTES_CROSS_ORGANIZATION"} = 16;
$objTrustAttrHash{"ATTRIBUTES_WITHIN_FOREST"} = 32;
$objTrustAttrHash{"ATTRIBUTES_TREAT_AS_EXTERNAL"} = 64;

my $objRootDSE = Win32::OLE->GetObject("LDAP://$strDomain/RootDSE");
my $objTrusts  = Win32::OLE->GetObject("LDAP://cn=System," . 
                            $objRootDSE->Get("defaultNamingContext") );
print "Trusts for " . $strDomain . ":\n";

foreach my $objTrust ( in $objTrusts ) {
   next unless lc $objTrust->Class eq "trusteddomain";
   foreach my $strFlag (keys %objTrustDirectionHash) {
      if ($objTrustDirectionHash{$strFlag} == $objTrust->Get("trustDirection") ) {
         $strTrustInfo = $strTrustInfo . $strFlag . " ";
      }
   }

   foreach my $strFlag (keys %objTrustTypeHash) {
      if ($objTrustTypeHash{$strFlag} == $objTrust->Get("trustType") ) {
         $strTrustInfo = $strTrustInfo . $strFlag . " ";
      }
   }

   foreach my $strFlag (keys %objTrustAttrHash) {
      if ($objTrustAttrHash{$strFlag} == $objTrust->Get("trustAttributes") ) {
         $strTrustInfo = $strTrustInfo . $strFlag . " ";
      }
   }

   print " " , $objTrust->Get("trustPartner") , " : " , $strTrustInfo, "\n";
   $strTrustInfo = "";
}

This code has been viewed 1280 times.

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