#!/usr/bin/perl
 
use strict;
use Win32::OLE;
$Win32::OLE::Warn = 3;
 
# ------ SCRIPT CONFIGURATION ------
my $strComputer = 'www1-ext';
my $domain_regex = '^www.[n-z].+$';
my $new_root_path = "\\\\web\\customer-home";
 
my $objIIS = Win32::OLE->GetObject('IIS://' . $strComputer . '/W3SVC');
foreach my $objSite (in $objIIS) {
    if (($objSite->Class eq 'IIsWebServer')) {
 
        if($objSite->ServerComment =~ /$domain_regex/){
            my $objVdir  =  $objSite->GetObject("IISWebVirtualDir","Root");
            my @explode = split(/\\/,$objVdir->{'Path'});
            my $alpha = substr($explode[$#explode],0,1);
            if($alpha =~ /\d+/){
                $alpha = "0-9";
            }
            my $old_path = $objVdir->{'Path'};
            my $new_path = "$new_root_path\\$alpha\\$explode[$#explode]";
            print $objSite->ServerComment . ' (' . $objSite->Name . ') - '.$explode[$#explode], "\n";
            print "\tOld Path: $old_path\n";
            print "\tNew Path: $new_path\n";
            $objVdir->{Path} = $new_path;
            $objVdir->SetInfo();
        }
    }
}