adric: (Hacker)
adric ([personal profile] adric) wrote2007-10-04 09:49 pm
Entry tags:

WMI, IIS, CIM and other cursewords

Finally, after hours of reading through mind-numbingly overcomplicated documentation about the incredible plural architectures for Windows system management (not that I wanted to know any of that, mind you) and their various benighted APIs and tools ...

I present, the answer, or at least the kernel of one:

// 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);


ref: http://msdn2.microsoft.com/en-us/library/ms524913.aspx