#  IISProperty.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 sets a property in a web server.
#
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 );

Configure( \%Config );
push( @ARGV, Win32::DomainName() ) 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 $fDirty = 0;

  print "\n$Object->{ADsPath}\n";
  foreach my $Property ( sort( keys( %{$Config{prop}} ) ) )
  {
    my $Value = $Config{prop}->{$Property};
    next unless( defined $Object->{$Property} );

    if( $Config{set} )
    {
      print "  Setting '$Property' => '$Value'\n";
      $Object->{$Property} = $Value;
      $fDirty = 1;
    }
    else
    {
      print "  $Property: $Object->{$Property}\n";
    }
  }
  $Object->SetInfo() if( $fDirty );
}

sub Configure
{
    my( $Config ) = @_;
    my $Result;
    Getopt::Long::Configure( "prefix_pattern=(-|\/)" );
    $Result = GetOptions( $Config,
              qw(
                prop|p=s%
                set|s
              ) );
  return();
}
