d

Create new Web Server Instance on IIS

Dan Casier sent me this script that will create a website and appropriate
DNS record. The script is intended for Windows 2000 Server with local DNS
and the necessary DNS mof installed.

/**  Script:  MakeWebSite.js
*
*    Purpose: Makes a website on a Windows 2000 server, the coresponding
*             resource record in the DNS and the folder that conteints the website.
*    Author:  Dan Casier
*    Date:    30 oct 2004
*    Web:     none
*    Note:    runs on a Windows 2000 server with IIS and DNS installed.
*             not tested on Windows 2003 server
              //** do not forget to give permissions for IUSR-users to the folder.
*             creating a website this way takes less then a second.
*
*    Copyright 2004 Dan Casier
*
**/
var Web           = "examplesite";                     //name of the website
var strRRsufix    = ". IN A 192.168.51.200";           //ip address of the IIS server
var ServerBinding = "192.168.001.051:80:";             //ip address and portnumber
var DNSroot       = ".domain.com";
var strDomain     = "domain.com";
var sRootDir      = "c:\\websites\\mtest\\iis2";       //place of the site

CreateWeb(Web + DNSroot,sRootDir,Web);

function CreateWeb( sHostName, sRootDir, comments )
{
 var oWeb = GetObject("IIS://localhost/W3SVC");
 oWeb.GetInfo();
 //looks for the first free website and create it
 var Index = 1;
 var cont  = 0;
 while( cont == 0 ) 
 {
  try 
  {
    var webobj = GetObject("IIS://localhost/w3svc/" + Index);
  }
  catch( e )  
  {
    if( ( e.number & 0xFFFF ) > 0 )
    {
      cont= 1;
    }
  }
  Index = Index + 1;
 }
 Index = Index - 1;
 WScript.Echo( "Next Index: " + Index );
 WScript.Echo("creating web " + sHostName);
 oServer = oWeb.Create("IIsWebServer",Index);

 //Configure the new server
 oServer.DefaultDoc        = "default.htm, index.htm";
 oServer.ServerComment     = comments;
 oServer.ConnectionTimeout = 600;
 oServer.ServerBindings    = ServerBinding + sHostName;
 oServer.SetInfo();
 
 var fs    = new ActiveXObject("Scripting.FileSystemObject");
 if( !fs.FolderExists( sRootDir ) )
  var foldr = fs.CreateFolder(sRootDir);
 
 //Create virtual root directory for the new site
 oServer   = GetObject("IIS://localhost/w3svc/" + Index );
 var oVdir = oServer.Create("IIsWebVirtualDir", "ROOT");
 
 //Configure the virtual root directory
 oVdir.Path         = sRootDir;
 oVdir.AccessRead   = "True";
 oVdir.AccessWrite  = "True";
 oVdir.AccessScript = "True";
 oVdir.SetInfo();
 
 //Starting the website
 WScript.Echo("starting web " + sHostName + " " + Index);
 oServer.Start();
 
 //add DNS-record
 strRR=sHostName + strRRsufix;
 var objDNS       = GetObject("winmgmts:{impersonationLevel=impersonate}!\\\\.\\root\\MicrosoftDNS");
 var objRR        = objDNS.Get("MicrosoftDNS_ResourceRecord");
 var objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name=\".\"");
 var strNull      = objRR.CreateInstanceFromTextRepresentation(objDNSServer.Name, strDomain, strRR);

} //end function