Share via


IIsWebService.CreateNewSite (WMI)

You can use the CreateNewSite method to create a new Web site on an IIS 6.0 server. CreateNewSite works in the following way:

  • If the method succeeds, it returns a reference to the site identification number for the newly created site.
  • If the method fails to create a site, it returns an error. This may happen if the ServerId does not exist. If a site already exists with the ServerComment that you want to use, an error is not returned because the new site can still be created with a unique site identification number. See the Remarks.
Syntax

return_value = object.CreateNewSite

(
StringServerComment,
ServerBinding ServerBindings[],
String PathOfRootVirtualDir,
[SINT32ServerId]
)

Parameters

ServerComment [in]
Contains a string representing the ServerComment for the new Web site. In the IIS Manager, this would be the Description field under Web Site Identification in a Web Site property sheet. for the new Web site. In the IIS Manager, this would be the Description field under Web Site Identification in a Web Site property sheet. for the new Web site. In the IIS Manager, this would be the Description field under Web Site Identification in a Web Site property sheet.ServerBindings[] [in]
Contains an array of instances of the ServerBinding class specifying what server bindings you want on this site. Each ServerBinding instance includes at least one of the host name, port, and IP address.PathOfRootVirtualDir [in]
Contains a string representing a fully qualified path to a physical directory to which you want the Web site mapped.ServerId [in,optional]
An optional string parameter containing the site identification number which you want the method to use. If this parameter is empty, a hash function is used to create the site identification number. See the information above.

Remarks

In the IIS Metabase, Web sites are represented by the service type, and a unique site identification number. For example, on a new installation of IIS, the Default Web Site is represented in ADSI as w3svc/1.

In IIS versions 5.1 and previous, the site identification number increments by one for each new site. For example, if you create a Web site called Contoso which becomes w3svc/3, the next Web site you create will become w3svc/4.

CreateNewSite allows you to specify the site identification number that you want to use. If you do not specify a site identification number, the method performs a hash function on the ServerComment passed to the method and creates a new number. If there are multiple sites with the same ServerComment, the hash function detects a collision and selects the next available site identification number. For example, create a site with the following line of code:

  myNewSiteID = IIsWebServiceObj.CreateNewSite("MyNewSite", Bindings, "C:\Inetpub\Wwwroot")

If there is already a Web site on the server called MyNewSite whose site identification number is 5555, then CreateNewSite will return 5556 if 5556 is not already taken.

If you create duplicate sites on multiple IIS 6.0 servers using the IIS Manager or the CreateNewSite method, there is a good chance that the site identification numbers will be the same on each server for each duplicated site. These servers can then be included in a Microsoft Application Center Web farm without problems.

Code Example
  ' Make connections to WMI, to the IIS namespace on MyMachine, and to the Web service.
set locatorObj = CreateObject("WbemScripting.SWbemLocator")
set providerObj = locatorObj.ConnectServer("MyMachine", "root/MicrosoftIISv2")
set serviceObj = providerObj.Get("IIsWebService='W3SVC'")

' Build binding object, which is a required parameter of the CreateNewSite method.
' Use the SpawnInstance WMI method since we are creating a new instance of an object.
Bindings = Array(0)
Set Bindings(0) = providerObj.get("ServerBinding").SpawnInstance_()
Bindings(0).IP = ""
Bindings(0).Port = "8383"
Bindings(0).Hostname = ""

' Create the new Web site using the CreateNewSite method of the IIsWebService object.
Dim strSiteObjPath
strSiteObjPath = serviceObj.CreateNewSite("MyNewSite", Bindings, "C:\Inetpub\Wwwroot")

A more complete example is in the Lesson 3 of Module Three of the IIS WMI Provider Tutorial.