2

Is there a way to script the creation of an IIS 6 website? I don't have access to powershell, so it would have to use regular commands, or cscript scripts.

1
  • To complement this question and the answers (as the title doesn't specify IIS6), anyone using IIS7 or above should take a look at appcmd and/or the adminpak for IIS7 where there's a Configuration Editor that generates scripts like this in a number of languages. Oct 26, 2009 at 14:40

3 Answers 3

4

This method works on Windows Server 2003 IIS 6.0:

strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
        & strComputer & "\root\microsoftiisv2")

Set objWebService = objWMIService.ExecQuery _
    ("Select * From IISWebService")

arrBindings = Array(0)
Set arrBindings(0) = _
    objWMIService.Get("ServerBinding").SpawnInstance_()
arrBindings(0).IP = "192.168.1.1"
arrBindings(0).Port = "80"
arrBindings(0).Hostname = "www.example.com"

For Each objItem in objWebService
    objItem.CreateNewSite "Test Site", arrBindings, _
        "c:\inetpub\wwwroot\testsite"
Next

Here is the link to the Microsoft TechNet Script Repository related to or Managing Web sites on Internet Information Server 6.0.

Another useful TechNet article explains how to create an AppPool programmatically.


Here is an alternative method, which works on Windows Server 2000 (IIS 5.0) too. Write a .vbs file:

IISWebName = "Name of the Web"
IISWebPath = "D:\Websites\MyWeb"
IISWebHeader = "http://www.example.com"
IISIPAddress = "10.0.0.10"

Set wsShell = CreateObject("WScript.Shell") 
wsShell.Run "iisweb.vbs /create """ & IISWebPath & """ """ & IISWebName & """ /b 80 /i """ & IISIPAddress & """" /d """ & IISWebHeader & """"

If you want to add virtual directories, you could do it this way:

oShell2.Run "iisvdir.vbs /create """ & IISWebName & """ pfengine ""D:\Common\engine"""
1
  • That script repository is really useful.
    – Cheeso
    Jul 15, 2009 at 12:06
1

You'll probably want to customise the website as well rather than leaving at its default settings. After much scrabbling around, this is what I came up with:

Sub CreateWebsite(name, hostName, physicalPath)
    ' Make connections to WMI, to the IIS namespace on the local machine. Then grab a reference to the WWW service 
    Dim locatorObj : set locatorObj = CreateObject("Wbemscripting.SWbemLocator") 
    Dim providerObj : set providerObj = locatorObj.ConnectServer(".", "root/MicrosoftIISv2") 
    Dim serviceObj : set serviceObj = providerObj.Get("IIsWebService='W3SVC'") 

    'Create a new instance as per splattne's answer
    Dim Bindings : Bindings = Array(0) 
    Set Bindings(0) = providerObj.get("ServerBinding").SpawnInstance_() 
    Bindings(0).IP = "" 
    Bindings(0).Port = "80" 
    Bindings(0).Hostname = hostName

    ' Create the new Web site using the CreateNewSite method of the IIsWebService object. 
    Dim newSitePath : newSitePath = serviceObj.CreateNewSite(name, Bindings, physicalPath) 

    ' CreateNewSite returns a string reference to the created web service. To alter the settings we need to create a string which references the root virtual directory settings.
    Dim settingsPath : settingsPath = Replace(Left(newSitePath, Len(newSitePath) - 1) & "/ROOT'", "IIsWebServer","IIsWebVirtualDirSetting")

    ' Grab a reference to the settings
    Dim settings : set settings = providerObj.get(settingsPath)

    ' By comparing the settings of an existing, manually set up site, and one created with Splattne's method I realised I needed to change the following
    settings.AspEnableParentPaths = True
    settings.AccessFlags = 512
    settings.AccessRead = True
    settings.AuthAnonymous = True
    settings.AuthFlags = 5
    settings.AuthNTLM = True
    settings.AccessScript = True
    settings.AppFriendlyName = name
    settings.Put_()

    ' Set a custom handler for 500 errors. In this case a url called 500.asp. This is a bit hacky but it works.
    settings.HttpErrors(41).Properties_("HttpErrorCode") = 500
    settings.HttpErrors(41).Properties_("HttpErrorSubcode") = "*"
    settings.HttpErrors(41).Properties_("HandlerType") = "URL"
    settings.HttpErrors(41).Properties_("HandlerLocation") = "/500.asp"
    settings.Put_()

    ' Start the service
    Dim newSite: set newSite = providerObj.Get(newSitePath)
    newSite.Start
End Sub
0

I think PowerShell is really for IIS 7. Are you sure you want IIS 6 and not IIS 7? IIS 7 has the appcmd command which is somewhat difficult to use but works.

1
  • Powershell is for Windows administration. It's not just IIS6. It's included in Win Server 2008, which also includes IIS7. But Powershell can be installed and used just fine on Win Server 2003, which includes IIS6.
    – Cheeso
    Jul 15, 2009 at 12:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .