#  DisplayADSIDomains2.pl
#  Example 9.4:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
#  This script displays the domains visible to the local machine along
#  with their properties by using ADSI.
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";


use Win32::OLE qw( in );

$ADsPath = "WinNT:";
if( $AD = Win32::OLE->GetObject( $ADsPath ) )
{
  my $iCount = 0;
  my $Schema;
  my $PropertyList;
  
  print "This machine can see the following domains:\n";
  foreach my $Domain ( in( $AD ) )
  {
    $PropertyList = GetProperties( $Domain ) unless( ref $PropertyList );
    print ++$iCount . ") $Domain->{Name}\n";
    foreach my $Property ( @{$PropertyList} )
    {
      # Check that $Domain is indeed a domain and not
      # a workgroup.
      if( ! defined $Domain->{$Property} )
      {
        print "\tCan not query domain properties.\n";
        print "\tThis must be a workgroup.\n";
        last;
      }
      print "\t $Property: $Domain->{$Property}\n";
    }
  }
}

sub GetProperties
{
  my( $Obj ) = @_;
  my @Properties;
  
  if( my $Schema = Win32::OLE->GetObject( $Obj->{Schema} ) )
  {
    foreach my $PropList ( $Schema->{MandatoryProperties},
                           $Schema->{OptionalProperties} )
    {
      push( @Properties, @{$PropList} ) if( defined $PropList );
    }
  }
  return( \@Properties );
}
