Category: Utility Script
Author/Contact Info Billy Strader
Description: OK I had this problem in IIS where I would set a redirect on a top lvl and it wouldn't proprogate down and redirect everything under my directory. I found if I went in and manually switched each directory it worked. However this could take hours if there were hundreds of directories like I had. So I found I could do this with Perl using the Win32::OLE module. Here is my code. Please note... It is very ugly and not very dynamic... I made it primarly for 1 directory... But if others wanted to modify it so it is more dynamic please do and post it...
#!perl -w
use strict;
use Carp;
use Win32;
use Win32::OLE;

printdir(@ARGV);

my $newitem;

sub printdir {
    my $item;
    foreach $item(@_) {
        next if $item =~ /\.{1,2}$/;
        if (-d $item) {
            $newitem = $item;
            $newitem =~ s/\A[^\/]+//;
            &SetRedirect($newitem);
            print "Processed $newitem\n";
            opendir(SUBDIR, $item) or croak "Can't open directory :$!"
+;
            my @subdir_items = readdir(SUBDIR);      #+
            closedir(SUBDIR);                        #+  
            printdir(map {"$item/$_"}@subdir_items); #+
        }
    }
}

sub SetRedirect {
    my $newdir = shift;
    my $hostname = "my.server.com";
    my $websvc=Win32::OLE -> GetObject("IIS://$hostname/W3SVC/1");
    
    # Get root of Default Web Site
    my $vRoot = $websvc->GetObject("IIsWebVirtualDir", "Root") || die 
+"Could not create root of Default Web Site object: $!";

    
    my $root = "thedir$newdir";

    if ($newdir) {
        my $vDir = $vRoot->Create("IIsWebVirtualDir", $root);

        # Print out the current value of some properties:
        print "Before: " . $vDir->{"HttpRedirect"} . "\n";

        # Set some properties:
        $vDir->{"HttpRedirect"}="https\://my\.server\.com/thedir\$S\$Q
+, EXACT_DESTINATION";

        # Save the property changes in the metabase:
        $vDir->SetInfo();

        print "After: " . $vDir->{"HttpRedirect"} . "\n";
    }
}