Creating Sites and Virtual Directories, and Setting Properties Using WMI

Because everything in WMI is connected by object relationships, you need a connection to a service to create a site, and you need a connection to a site to create a virtual directory. It makes sense that the methods that create the child objects are in the parent objects. As you notice in the example below, the WMI method SpawnInstance_ is used to create a new instance of the ServerBinding object, instead of using a method from a parent object. This is because the elements of the IIsStructuredDataClass, such as ServerBinding, are on their own.

IIS 5.1 and earlier: The IIS WMI provider is not available.

Before looking at the following code, read about the SpawnInstance_ method of the SWbemObject object in the WMI SDK. Also look at the IIsWebService.CreateNewSite (WMI) method of the IIsWebService (WMI) object. Remember to change the string MyMachine to the name of a machine running IIS 6.0. Also, change the path C:\Inetpub\Wwwroot to a valid path.

' 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") 
If Err Then 
WScript.Echo "*** Error Creating Site: " & Hex(Err.Number) & ": " & Err.Description & " ***" 
WScript.Quit(1) 
End If 
    
' strSiteObjPath is in the format of IIsWebServer='W3SVC/1180970907' 
' To parse out the absolute path, W3SVC/1180970907, use the SWbemObjectPath WMI object. 
Set objPath = CreateObject("WbemScripting.SWbemObjectPath") 
objPath.Path = strSiteObjPath 
strSitePath = objPath.Keys.Item("") 
    
' Set some properties on the root virtual directory which was created by CreateNewSite. 
Set vdirObj = providerObj.Get("IIsWebVirtualDirSetting='" & strSitePath & "/ROOT'") 
vdirObj.AuthFlags = 5 ' AuthNTLM + AuthAnonymous 
vdirObj.EnableDefaultDoc = True 
vdirObj.DirBrowseFlags = &H4000003E ' date, time, size, extension, longdate 
vdirObj.AccessFlags = 513 ' read, script 
vdirObj.AppFriendlyName = "Root Application" 
    
' Save the new settings to the metabase 
vdirObj.Put_() 
    
' CreateNewSite does not start the server, so start it now. 
Set serverObj = providerObj.Get(strSiteObjPath) 
serverObj.Start 
    
WScript.Echo "A New site called MyNewSite was created with the path and unique site identification number of " & strSitePath
// Make connections to WMI, to the IIS namespace on MyMachine, and to the Web service. 
var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator"); 
var providerObj = locatorObj.ConnectServer("MyMachine", "root/MicrosoftIISv2"); 
var 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. 
var Bindings = new Array(); 
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. 
try { 
var strSiteObjPath; 
strSiteObjPath = serviceObj.CreateNewSite("MyNewSite", Bindings, "C:\\Inetpub\\Wwwroot"); 
} 
catch(e) { 
WScript.Echo("*** Error Creating Site: " + e + " " + e.Number + ", " + e.Description + " ***"); 
WScript.Quit(1); 
} 
 
// strSiteObjPath is in the format of IIsWebServer='W3SVC/1180970907' 
// To parse out the absolute path, W3SVC/1180970907, use the SWbemObjectPath WMI object. 
var objPath = new ActiveXObject("WbemScripting.SWbemObjectPath"); 
objPath.Path = strSiteObjPath; 
strSitePath = objPath.Keys.Item(""); 
 
// var some properties on the root virtual directory which was created by CreateNewSite. 
var vdirObj = providerObj.Get("IIsWebVirtualDirSetting='" + strSitePath + "/ROOT'"); 
vdirObj.AuthFlags = 5; // AuthNTLM + AuthAnonymous 
vdirObj.EnableDefaultDoc = true; 
vdirObj.DirBrowseFlags = 0x4000003E; // date, time, size, extension, longdate 
vdirObj.AccessFlags = 513; // read, script 
vdirObj.AppFriendlyName = "Root Application"; 
 
// Save the new settings to the metabase 
vdirObj.Put_(); 
 
// CreateNewSite does not start the server, so start it now. 
var serverObj = providerObj.Get(strSiteObjPath); 
serverObj.Start; 
 
WScript.Echo("A New site called MyNewSite was created with the path and unique site identification number of " + strSitePath);

If you run this script twice, you get the following error in the command-prompt window:

SWbemObjectEx: You were not connected because a duplicate name exists on the network. Go to System in Control Panel to change the computer name and try again. 

The error in the command-prompt window is caused by both sites using the same port number in the server bindings, which means that both sites cannot run at the same time. (For more information on port numbers, see Name Resolution in IIS Help which is accessible from IIS Manager.) However, the two Web sites are created successfully, and as the help topics for CreateNewSite states, each site is created with a unique site identification number.

The following line in the VBScript example may seem complicated:

Set Bindings(0) = providerObj.get("ServerBinding").SpawnInstance_() 

However, it just combines the following two lines:

Set ServerBinding = providerObj.get("ServerBinding") 
Set Bindings(0) = ServerBinding.SpawnInstance_() 

The following code example creates a virtual directory and sets some necessary properties.

const APP_POOLED = 2 
var strNewVdir 
strNewVdir = "W3SVC/1/Root/newVdir" 
    
' Make connections to WMI, to the IIS namespace on MyMachine. 
set locatorObj = CreateObject("WbemScripting.SWbemLocator") 
set providerObj = locatorObj.ConnectServer("MyMachine", "root/MicrosoftIISv2") 
    
' Add a virtual directory to the site. This requires SpawnInstance(). 
Set vdirClassObj = providerObj.Get("IIsWebVirtualDirSetting") 
Set vdirObj = vdirClassObj.SpawnInstance_() 
vdirObj.Name = strNewVdir 
vdirObj.Path = "C:\Inetpub\Wwwroot" 
vdirObj.AuthFlags = 5 ' AuthNTLM + AuthAnonymous 
vdirObj.EnableDefaultDoc = True 
vdirObj.DirBrowseFlags = &H4000003E ' date, time, size, extension, longdate 
vdirObj.AccessFlags = 513 ' read, script 
    
' Save the new settings to the metabase 
vdirObj.Put_() 
    
' Create a pooled application on the new virtual directory. 
Set vdirObj = providerObj.Get("IIsWebVirtualDir='" & strNewVdir & "'") 
vdirObj.AppCreate2(2) 
    
' Set the application name. 
Set vdirObj = providerObj.Get("IIsWebVirtualDirSetting='" & strNewVdir & "'") 
vdirObj.AppFriendlyName = "Root Application" 
vdirObj.Put_()
var APP_POOLED = 2; 
var strNewVdir = "W3SVC/1/Root/newVdir"; 
 
// Make connections to WMI, to the IIS namespace on MyMachine. 
var locatorObj = new ActiveXObject("WbemScripting.SWbemLocator"); 
var providerObj = locatorObj.ConnectServer("MyMachine", "root/MicrosoftIISv2"); 
 
// Add a virtual directory to the site. This requires SpawnInstance(). 
var vdirClassObj = providerObj.Get("IIsWebVirtualDirSetting"); 
var vdirObj = vdirClassObj.SpawnInstance_(); 
vdirObj.Name = strNewVdir; 
vdirObj.Path = "C:\\Inetpub\\Wwwroot" 
vdirObj.AuthFlags = 5; // AuthNTLM + AuthAnonymous 
vdirObj.EnableDefaultDoc = true; 
vdirObj.DirBrowseFlags = 0x4000003E; // date, time, size, extension, longdate 
vdirObj.AccessFlags = 513; // read, script 
 
// Save the new settings to the metabase. 
vdirObj.Put_(); 
 
// Create a pooled application on the new virtual directory. 
var vdirObj = providerObj.Get("IIsWebVirtualDir='" + strNewVdir + "'"); 
vdirObj.AppCreate2(2); 
 
// Set the application name. 
var vdirObj = providerObj.Get("IIsWebVirtualDirSetting='" + strNewVdir + "'"); 
vdirObj.AppFriendlyName = "Root Application"; 
vdirObj.Put_();

In either of these examples, you could create sites in your FTP, SMTP, or NNTP service by replacing references to IIsWebService, IIsWebServer, and IIsWebVirtualDir with the appropriate object name as listed in the following table.

Web Objects

FTP Objects

SMTP Objects

NNTP Objects

IIsWebService

IIsFtpService

IIsSmtpService

IIsNntpService

IIsWebServiceSetting

IIsFtpServicSetting

IIsSmtpServiceSetting

IIsNntpServiceSetting

IIsWebServer

IIsFtpServer

IIsSmtpServer

IIsNntpServer

IIsWebServerSetting

IIsFtpServerSetting

IIsSmtpServerSetting

IIsNntpServerSetting

IIsWebVirtualDir

IIsFtpVirtualDir

IIsSmtpVirtualDir

IIsNntpVirtualDir

IIsWebVirtualDirSetting

IIsFtpVirtualDirSetting

IIsSmtpVirtualDirSetting

IIsNntpVirtualDirSetting