#  IISWebDump.pl
#  Example 9.10:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
#  This script displays IIS web site configurations.
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";


use Win32;
use Win32::OLE qw( in );

push( @ARGV, Win32::NodeName() ) if( ! scalar @ARGV );
foreach my $Machine ( @ARGV )
{
  my $ADsPath = "IIS://$Machine";
  if( my $IIS = Win32::OLE->GetObject( $ADsPath ) )
  {
    ProcessObject( $IIS );
  }
}

sub ProcessObject
{
  my( $Object ) = @_;
  local $PropertyName;
  my $iCount = 0;
  
  print "\nName:  $Object->{Name}\n";
  print "Class: $Object->{Class}\n";
  print "Path:  $Object->{ADsPath}\n";
  print "    This object contains the following sub objects:\n";
  print "    -----------------------------------------------\n";
  foreach my $Sub ( in( $Object ) )
  {
    print "    ", ++$iCount, ") ";
    print "$Sub->{Name} ($Sub->{Class}) $Sub->{ADsPath}\n";
  }
  print "\n";
  
  $~ = PROPERTY_HEADER_FORMAT;
  write;
  $~ = PROPERTY_FORMAT;
  foreach $PropertyName ( sort( @{GetProperties( $Object )} ) )
  {
    next if( "" eq $PropertyName );
    local( $Value ) = $Object->{$PropertyName};
    if( "ARRAY" eq ref $Value )
    {
      $Value = join( "\n", @{$Value} );
    }
    write;
  }
  
  # Recursively process each sub object
  foreach my $Sub ( in( $Object ) )
  {
    ProcessObject( $Sub );
  }
}

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 );
}

format PROPERTY_HEADER_FORMAT = 
    Property                    Value
    --------------------------- --------------------------------------
.
format PROPERTY_FORMAT =
    @<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    $PropertyName,              $Value
~                               ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                $Value
~                               ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                $Value
~                               ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                $Value
~                               ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                $Value
~                               ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                $Value
~                               ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                $Value
~                               ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                $Value
~                               ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                $Value
~                               ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                $Value
~                               ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                                $Value
.    