Creating a Web SiteThe 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 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 InformationThe 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.
Reference InformationThe 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.
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 InformationIn 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 |