Category: Utility Scripts
Author/Contact Info Billy Strader straderb@mindspring.com
Description: Ever needed to get your IIS metabase copied to your DR server nightly? Ever just needed to do a quick export of your IIS settings and change some stuff and put it on a new/differant system. Well I finally found out how to do it on IIS 6.0 on a Windows 2003 server. So that is what this script does. I hope you all enjoy.
#!/usr/bin/perl -w
#####################################
#
# Script Name: iisexport.pl
# Written by: Billy Strader
# E-Mail Address: losts@mindspring.com
# Purpose: To export IIS setting and import to another box such as a D
+R box.
# Notes:  This script ONLY works on Windows 2003 Server with IIS 6.0.
#         The account that runs this script will need Administrator ac
+cess to both servers.
#
#####################################

use strict;
use File::Copy;
use Win32::Service;

# Set the main server you wish to export data for
my $server = "myserver";

# Set the server you wish to import the data to
my $drserver = "mydrserver";

# Set the domain
my $domain = "mydomain.com";

# Set any variables such as IP's you wish to have changed from 1 syste
+m to the other
my %ips = ( "192.168.0.1" => "192.168.0.100",
          "192.168.0.2" => "192.168.0.200");

# First lets remove the old files if they exist
if (-f "\\\\$server.$domain\\c\$\\$server.xml") {
    unlink("\\\\$server.$domain\\c\$\\$server.xml");
}
if (-f "\\\\$server.$domain\\c\$\\new$server.xml") {
    unlink("\\\\$drserver.$domain\\c\$\\new$server.xml");
}
if (-f "\\\\$drserver.$domain\\c\$\\$server.xml") {
    unlink("\\\\$drserver.$domain\\c\$\\$server.xml");
}
if (-f "\\\\$drserver.$domain\\c\$\\new$server.xml") {
    unlink("\\\\$drserver.$domain\\c\$\\new$server.xml");
}

# Now lets runs the iiscnfg command to export the entire IIS setup
system("iiscnfg /s $server.$domain /export /f c:\\$server.xml /sp /lm 
+/children");

# Open the file and dump it to an array so we can go through it
open(FILE, "c:\\$server.xml");
my @file = <FILE>;
close(FILE);

# Create new file and for each entry in the replace catagory lets repl
+ace those selected items.
open(NEWFILE, ">c:\\new$server.xml");
foreach my $line (@file) {
    chomp $line;
    foreach my $ip (sort(keys %ips)){
        my $newip = $ips{$ip};
        $line =~ s/$ip/$newip/ig;
    }
    print NEWFILE "$line\n";
}
close(NEWFILE);

# Lets now copy the new file to the DR server
my $srcfile = "\\\\$server.$domain\\c\$\\new$server.xml";
my $dstfile = "\\\\$drserver.$domain\\c\$\\new$server.xml";
copy($srcfile, $dstfile) or die "Copy failed: $!";

# Now we import the IIS setting to the DR box with the changes
system("iiscnfg /s $drserver.$domain /import /f c:\\new$server.xml /sp
+ /lm /dp /lm /children");

# Here we go back through the XML file and start each instance using I
+ISWEB
my $inserver = 0;
foreach my $newline (@file) {
    chomp $newline;
    if ($newline =~ /ServerBindings/i) {
        print "$newline\n";
        $inserver  = 1;
    }
    if (($newline =~ /ServerComment/i) and ($inserver == 1)) {
        my ($com, $inst) = split(/=/,$newline);
        system("iisweb /start $inst /s $drserver.$domain");
        $inserver  = 0;
    }
}