共用方式為


Using WMI Query Language to Filter IIS Metabase Data

In the WMI SDK, a method called ExecQuery exists for the C++ Provider object, the COM+ IWbemServices object, and the scripting SWbemServices object. You can use this method to query a data store, such as the metabase, just as you can query a SQL or Access database. The query string uses the WMI Query Language (WQL) as explained in the WMI SDK under WMI Development Environment, WMI Query Language. Read the first three documents which guide you through building a query string in WQL, and then read the section on Data Queries.

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

The following code example uses the SELECT statement to enumerate all the services on your server.

for each item in GetObject("winmgmts://MyMachine").ExecQuery("select * from win32_service" ) 
  WScript.Echo item.name 
next
var e = new Enumerator(GetObject("winmgmts://MyMachine").ExecQuery("select * from win32_service" )); 
for(; ! e.atEnd(); e.moveNext()) 
{ 
  x = e.item(); 
  WScript.Echo(x.Name); 
}

You can do the same thing for objects in the IIS WMI provider. The following code sample enumerates all the instances of objects on your server that belong to the CIM_Setting class.

for each item in GetObject("winmgmts://MyMachine/root/MicrosoftIISv2").ExecQuery("select * from CIM_Setting" ) 
  WScript.Echo item.name 
next 
var e = new Enumerator(GetObject("winmgmts://MyMachine/root/MicrosoftIISv2").ExecQuery("select * from CIM_Setting" )); 
for(; ! e.atEnd(); e.moveNext()) 
{ 
  x = e.item(); 
  WScript.Echo(x.Name); 
}

With the addition of a little extra code, you can print out all the properties in each of the instances. The on error resume next command is used because some properties are arrays of objects and need more specialized code to print them out.

on error resume next 
for each item in GetObject("winmgmts://MyMachine/root/MicrosoftIISv2").ExecQuery("select * from CIM_Setting" ) 
  WScript.Echo item.name 
  for each propSet in item.Properties_ 
    WScript.Echo " " & propSet.Name & " = " & propSet.Value 
  next 
next
var e = new Enumerator(GetObject("winmgmts://MyMachine/root/MicrosoftIISv2").ExecQuery("select * from CIM_Setting" )); 
for(; ! e.atEnd(); e.moveNext()) 
{ 
  x = e.item(); 
  WScript.Echo(x.Name); 
  var f = new Enumerator(x.Properties_) 
  for(; ! f.atEnd(); f.moveNext()) { 
    propSet = f.item(); 
    WScript.Echo(" " + propSet.Name + " = " + propSet.Value); 
  } 
}

Sometimes a server administrator wants to find an instance of an object with a property that has a specific value. For example, to determine which virtual directories have pooled applications, use the following code.

for each item in GetObject("winmgmts://MyMachine/root/MicrosoftIISv2").ExecQuery("SELECT * FROM IIsWebVirtualDir WHERE AppIsolated = 2" ) 
  WScript.Echo item.name 
next
var e = new Enumerator(GetObject("winmgmts://MyMachine/root/MicrosoftIISv2").ExecQuery("SELECT * FROM IIsWebVirtualDir WHERE AppIsolated = 2" )); 
for(; ! e.atEnd(); e.moveNext()) 
{ 
  x = e.item(); 
  WScript.Echo(x.Name); 
}