Previous Page
Next Page

Creating a Web Site

The advantage of using WMI to create Web sites is that it gives you a consistent product and vastly simplifies the creation process by automating dozens of minute details. For companies that create a lot of Web sites, scripting makes a lot of sense.

Just the Steps

To use WMI to create a Web site

1.
Define the appropriate variables.

2.
Use CreateObject to create an instance of the WbemScripting SWbemLocator object.

3.
Use the locator object so that you can use the ConnectServer method to connect to the MicrosoftIISv2 namespace on the target computer.

4.
Use the service object to get an instance of "IIsWebService='W3SVC'".

5.
Use the server binding object to set your bindings.

6.
Use the createNewSite method to create the Web site.


The following code is CreateSite.vbs. When run on a server that has IIS installed, it will create a Web site.

CreateSite.vbs

Option Explicit
'On Error Resume Next
Dim strComputer
Dim wmiNS
Dim siteName
Dim strSiteObjPath
Dim locatorObj
Dim providerObj
Dim objPath
Dim vDirObj
Dim serverObj
Dim serviceObj
Dim bindings
Dim strSitePath

strComputer = "."
wmiNS = "root/MicrosoftIISv2"
siteName = "LondonWebSite"

Set locatorObj = CreateObject("WbemScripting.SWbemLocator")
Set providerObj = locatorObj.ConnectServer _
  & (strComputer, wmiNS)
Set serviceObj = providerObj.Get _
  & ("IIsWebService='W3SVC'")
Set objPath = CreateObject("WbemScripting.SWbemObjectPath")

Bindings = Array(0)
Set Bindings(0) = providerObj.Get("ServerBinding") _
  & .SpawnInstance_()
Bindings(0).IP = ""
Bindings(0).Port = "8383"
Bindings(0).Hostname = ""

strSiteObjPath = serviceObj.CreateNewSite _
  & (siteName, Bindings, "C:\Inetpub\Wwwroot")
objPath.Path = strSiteObjPath
strSitePath = objPath.Keys.Item("")
  subCheckErrors

WScript.Echo "Created " & siteName
WScript.Echo "The path/ID is " & strSitePath

Sub subCheckErrors
  If Err Then
    WScript.Echo "Error: " & Hex(Err.Number) _
      & ": " & Err.Description
    WScript.Quit(1)
  End If
End Sub

Header Information

The Header information section of CreateSite.vbs includes a lot of variables. Understanding how to use these variables will further your understanding of the script. The variables used in this script are described in Table 19-2.

Table 19-2. Variables Used in CreateSite.vbs

Variable

Use

strComputer

Holds assignment of the target computer name

wmiNS

Holds the WMI namespace

siteName

Holds the name of the new Web site to create

strSiteObjPath

Holds the path to the new Web site

locatorObj

Holds the object that comes back from SWbemLocator

providerObj

Uses the object from locatorObj to make a connection to the server

objPath

Holds the object that comes back from SWbemObjectPath

serviceObj

Holds the object that comes back from the providerObj object to get an instance of IIsWebService=<;$QS>W3SVC<;$QS>

bindings

Holds the elements of the array that is used for ServerBinding

strSitePath

Holds the key items from objPath


Reference Information

The Reference information section in CreateSite.vbs is large. This section could be condensed somewhat by combining statements and pulling data directly into the script instead of first populating variables. However, reducing the code by a few lines would make a much less readable script. You begin the Reference information section of the script by assigning a value to strComputer. You then set the wmiNS variable to be equal to the root/MicrosoftIISv2 namespace. Note that the MicrosoftIISv2 namespace is under the root. It is not in root\cimv2, as many of your WMI scripts have been. You now assign a name to the siteName variable, which is the name of the Web site you will be creating.

We set the variable locatorObj to be equal to the object that comes back when you use CreateObject to create an instance of the SWbemLocator object. You need to create an instance of the SWbemLocator object so that you can gain access to the ConnectServer method. You use ConnectServer to connect to the root/MicrosoftIISv2 namespace on your target server. You use the variable providerObj to hold the object.

Quick Check

Q.

Why is it necessary in the CreateSite.vbs script to use the SWbemLocator object?

A.

The SWbemLocator object is necessary so that you can use the ConnectServer method that it exposes.

Q.

Where does the MicrosoftIISv2 namespace reside?

A.

The MicrosoftIISv2 namespace resides under the root WMI namespace.


You now set serviceObj equal to the object you get when you connect to the Web service on your London server. Once you make your connection to the Web service, you need to build a binding object. The binding object is a required parameter of the CreateNewSite method, and because it has multiple elements, it is stored as an array. SpawnInstance is the WMI method used because you're creating a new instance on an object. The Reference section is seen below.

strComputer = "."
wmiNS = "root/MicrosoftIISv2"
siteName = "LondonWebSite"
Set locatorObj = CreateObject("WbemScripting.SWbemLocator")
Set providerObj = locatorObj.ConnectServer _
  & (strComputer, wmiNS)
Set serviceObj = providerObj.Get _
  & ("IIsWebService='W3SVC'")
Set objPath = CreateObject("WbemScripting." _
  & "SWbemObjectPath")

Bindings = Array(0)
Set Bindings(0) = providerObj.Get("ServerBinding") _
  & .SpawnInstance_()
Bindings(0).IP = ""
Bindings(0).Port = "8383"
Bindings(0).Hostname = ""

Worker and Output Information

In the Worker and Output information section of the script, the Web site is created. The variable that holds the return information from using the CreateNewSite method of the IIsWebService object is strSiteObjPath. To call the CreateNewSite method, you have to specify the site name, the bindings, and the physical path for the files. The variable strSiteObjPath is in the format of IIsWebServer='W3SVC/1180970907'; therefore, to parse out the absolute path, you use the SWbemObjectPath WMI object.

After you complete parsing out the absolute path, you call the subCheckErrors subroutine. In the subCheckErrors subroutine, you check the err object and echo out both the number and description of the error.

The script ends by echoing out the completed site name as well as the path and the unique site ID number that was built by using the strSitePath variable. The Worker and Output section of the script is seen below.

strSiteObjPath = serviceObj.CreateNewSite _
  & (siteName, Bindings, "C:\Inetpub\Wwwroot")
objPath.Path = strSiteObjPath
strSitePath = objPath.Keys.Item("")
  subCheckErrors

WScript.Echo "Created " & siteName
WScript.Echo "The path/ID is " & strSitePath

Sub subCheckErrors
  If Err Then
    WScript.Echo "Error: " & Hex(Err.Number) _
      & ": " & Err.Description
    WScript.Quit(1)
  End If
End sub


Previous Page
Next Page