#  IISTreeDump.pl
#  Example 9.12:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
#  This script displays the IIS tree.
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";


use Getopt::Long;
use Win32;
use Win32::OLE qw( in );

%CHAR = (
  graphic =>  {
    horizontal  =>  "\xc4",
    vertical    =>  "\xb3",
    tee         =>  "\xc3",
    corner      =>  "\xc0"
  },
  ascii   =>  {
    horizontal  =>  "-",
    vertical    =>  "|",
    tee         =>  "+",
    corner      =>  "\\"
  }
);

Configure( \%Config );
if( $Config{help} )
{
  Syntax();
  exit();
}
$CharSet = ($Config{ascii})? $CHAR{ascii} : $CHAR{graphic};
push( @ARGV, Win32::NodeName() ) if( ! scalar @ARGV );
foreach my $Machine ( @ARGV )
{
  my $ADsPath = "IIS://$Machine";
  if( my $IIS = Win32::OLE->GetObject( $ADsPath ) )
  {
    ProcessDir( $IIS );
  }
}

sub ProcessDir
{
  my( $Object, $IndentString ) = @_;
  local $PropertyName;
  my @SubDirList = in( $Object );
  my $iCount = scalar @SubDirList;

  print $CharSet->{horizontal} x 3, $Object->{Name};
  if( ( "IIsWebServer" eq $Object->{Class} )
      || ( "IIsFtpServer" eq $Object->{Class} )
      || ( "IIsSmtpServer" eq $Object->{Class} )
      || ( "IIsNntpServer" eq $Object->{Class} ) )
  {
    print " ($Object->{ServerComment})";
  }
  print "\n";
  foreach my $Sub ( @SubDirList )
  {
    my $String;

    if( --$iCount )
    {
      $String .= "   $CharSet->{vertical}";
      print $IndentString, "   $CharSet->{tee}";
    }
    else
    {
      $String .= "    ";
      print $IndentString, "   $CharSet->{corner}";
    }
    ProcessDir( $Sub, $IndentString . $String );
  }
}


sub Configure
{
  my( $Config ) = @_;
  my $Result;

  Getopt::Long::Configure( "prefix_pattern=(-|\/)" );
  $Result = GetOptions( $Config,
                        qw(
                          ascii|a
                          help|?|h
                        )
  );
  push( @{$Config->{servers}}, @ARGV );
  if( ! scalar @{$Config->{servers}} )
  {
    push( @{$Config->{servers}}, Win32::NodeName() );
  }
  $Config->{help} = 1 unless( $Result );
}

sub Syntax
{
  my( $Script ) = ( Win32::GetLongPathName( $0 ) =~ /([^\\\/]*?)$/ );
  my( $Line ) = "-" x length( $Script );

  print <<EOT;

$Script
$Line
Displays an IIS server tree.

Syntax:
    perl $Script [-a] Server [ Server2 ... ]
        -a..........Uses ascii characters to display the IIS tree.
        Server......The name of an IIS server.
                    If no servers are specified then the local
                    machine is used.
EOT
}
