使用WMI脚本自动创建IIS站点
创建站点.vbs:此脚本可以成功创建站点(windows2003 iis6下验证成功),执行前需要先修改些配置信息(搜索文件中的todo);
1 ' http://doc.51windows.net/iismmc/?url=/iismmc/htm/ref_mof_iiswebservice_createnewsite.htm 2 ' 建立与 WMI、MyMachine 上的 IIS 名称空间以及 Web 服务的连接。 3 dim locatorObj,providerObj,serviceObj,Bindings 4 set locatorObj = CreateObject("WbemScripting.SWbemLocator") 5 set providerObj = locatorObj.ConnectServer("LOCALHOST", "root/MicrosoftIISv2") 6 set serviceObj = providerObj.Get("IIsWebService='W3SVC'") 7 8 ' 建立绑定对象,这是 CreateNewSite 方法必需的参数。 9 ' 由于我们正在创建某个对象的新实例,请使用 SpawnInstance WMI 方法。 10 Bindings = Array(0) 11 Set Bindings(0) = providerObj.get("ServerBinding").SpawnInstance_() 12 Bindings(0).IP = "" 13 'todo:端口号 14 Bindings(0).Port = "1009" 15 Bindings(0).Hostname = "" 16 17 ' 使用 IIsWebService 对象的 CreateNewSite 方法创建新网站。 18 Dim strSiteObjPath 19 'todo:网站名,网站路径 20 strSiteObjPath = serviceObj.CreateNewSite("GCPS_HRRQ", Bindings, "D:\GCPS\Output_GCPS_HRRQ\_PublishedWebsites\GCPS.Web") 21 If Err Then 22 WScript.Echo "*** Error Creating Site:" & Hex(Err.Number) & ":" & Err.Description & " ***" 23 WScript.Quit(1) 24 End If 25 26 ' strSiteObjPath 是以 IIsWebServer='W3SVC/1180970907' 格式表示的 27 ' 要解析出绝对路径 W3SVC/1180970907,请使用 SWbemObjectPath WMI 对象。 28 Set objPath = CreateObject("WbemScripting.SWbemObjectPath") 29 objPath.Path = strSiteObjPath 30 strSitePath = objPath.Keys.Item("") 31 32 ' 在由 CreateNewSite 创建的根虚拟目录上设置某些属性。 33 Set vdirObj = providerObj.Get("IIsWebVirtualDirSetting='" & strSitePath & "/ROOT'") 34 vdirObj.AuthFlags = 5 ' AuthNTLM + AuthAnonymous 35 vdirObj.EnableDefaultDoc = True 36 vdirObj.DirBrowseFlags = &H4000003E ' date, time, size, extension, longdate 37 vdirObj.AccessFlags = 513 ' read, script 38 vdirObj.AppFriendlyName = "默认应用程序" 39 'todo:默认应用程序池 40 vdirObj.AppPoolId = "GCPS_HRRQ" 41 vdirObj.DefaultDoc = Default.aspx,Default.htm,Default.asp,index.htm 42 43 ' 将新设置保存到配置数据库 44 vdirObj.Put_() 45 46 ' CreateNewSite 并没有启动服务器,因此现在要启动它。 47 Set serverObj = providerObj.Get(strSiteObjPath) 48 serverObj.Start 49 50 WScript.Echo "A New site was created with the path and unique site identification number of " & strSitePath
运行此脚本得到所有网站属性.vbs:此脚本可以列出网站站点所有属性,以进行高级设置;
'A New site called MyNewSite was created with the path and unique site identification number of W3SVC/456996467 On Error Resume Next set providerObj = GetObject("winmgmts://server-gcps-cs/root/MicrosoftIISv2") set IIsWebVirtualDirObj = providerObj.get("IIsWebVirtualDir='W3SVC/456996467/ROOT'") set IIsWebVirtualDirSettingObj = providerObj.get("IIsWebVirtualDirSetting='W3SVC/456996467/ROOT'") WScript.Echo "Read only properties of W3SVC/456996467/Root:" For Each Property in IIsWebVirtualDirObj.Properties_ WScript.Echo Property.Name & " = " & Property.Value Next WScript.Echo WScript.Echo "Read/Write properties of W3SVC/456996467/Root:" For Each Property in IIsWebVirtualDirSettingObj.Properties_ WScript.Echo Property.Name & " = " & Property.Value Next
注意:“W3SVC/456996467/Root”中的“W3SVC/456996467”应该是通过前面的脚本在cmd中运行后输出的值。