第 2 课:枚举要显示的属性

有时您需要查看特定节点上的所有属性而不必打开 IIS 管理器、配置数据库文件或 CIM Studio。下面的代码使用 VBScript For...Each 循环语句和 JScript Enumerator 对象显示默认网站的根虚拟目录的属性。在 VBScript 代码中,使用了 On Error Resume Next,因为某些属性是需要额外的代码才能显示的对象或对象阵列。

VBScript
On Error Resume Next
set providerObj = GetObject("winmgmts://janetfi-32/root/MicrosoftIISv2")
set IIsWebVirtualDirObj = providerObj.get("IIsWebVirtualDir='W3SVC/1/ROOT'")
set IIsWebVirtualDirSettingObj = providerObj.get("IIsWebVirtualDirSetting='W3SVC/1/ROOT'")
WScript.Echo "Read only properties of W3SVC/1/Root:"
For Each Property in IIsWebVirtualDirObj.Properties_
  WScript.Echo Property.Name & " = " & Property.Value
Next
WScript.Echo
WScript.Echo "Read/Write properties of W3SVC/1/Root:"
For Each Property in IIsWebVirtualDirSettingObj.Properties_
  WScript.Echo Property.Name & " = " & Property.Value
Next
JScript
var providerObj = GetObject("winmgmts://janetfi-32/root/MicrosoftIISv2");
var IIsWebVirtualDirObj = providerObj.get("IIsWebVirtualDir='W3SVC/1/ROOT'");
var IIsWebVirtualDirSettingObj = providerObj.get("IIsWebVirtualDirSetting='W3SVC/1/ROOT'");
WScript.Echo("Read only properties of W3SVC/1/Root:");
var e = new Enumerator(IIsWebVirtualDirObj.Properties_);
for(; ! e.atEnd(); e.moveNext())
{
  var Property = e.item();
  WScript.Echo(Property.Name + " = " + Property.Value);
}
WScript.Echo();
WScript.Echo("Read/Write properties of W3SVC/1/Root:");
var e = new Enumerator(IIsWebVirtualDirSettingObj.Properties_);
for(; ! e.atEnd(); e.moveNext())
{
  var Property = e.item();
  WScript.Echo(Property.Name + " = " + Property.Value);
}

要试用此示例,请将代码中的黑体部分更改为其他值,这些值在 KeyType 页上的“访问位置”表中列出。请记住,IIS 管理对象类型必须与配置数据库路径相匹配。

要将此代码转换为带有计算机名称、IIS 管理对象类型和配置数据库路径参数的工具,可以将上面的代码更改为下面的代码。黑体部分是新代码。

VBScript
On Error Resume Next
' 获取参数。
set WshArgs = WScript.Arguments
if WshArgs.Length = 3 then
dim machineName, adminObjType, metabasePath
machineName = WshArgs(0)
adminObjType = WshArgs(1)
metabasePath = WshArgs(2)
set providerObj = GetObject("winmgmts://" & machineName & "/root/MicrosoftIISv2")
set IIsWebVirtualDirObj = providerObj.get(adminObjType & "='" & metabasePath & "'")
set IIsWebVirtualDirSettingObj = providerObj.get(adminObjType & "Setting='" & metabasePath & "'")
WScript.Echo "Read only properties of " & metabasePath & ":"
For Each Property in IIsWebVirtualDirObj.Properties_
WScript.Echo Property.Name & " = " & Property.Value Next  WScript.Echo WScript.Echo "Read/Write properties of
" & metabasePath & ":"
For Each Property in IIsWebVirtualDirSettingObj.Properties_
WScript.Echo Property.Name & " = " & Property.Value
Next
' if WshArgs.Length = 3 then
else
WScript.Echo "Usage:"
WScript.Echo "cscript /nologo " & WScript.ScriptName & " <computer name> <admin object type> <metabase path>"
WScript.Echo "Example:"
WScript.Echo "cscript /nologo " & WScript.ScriptName & " MyMachine IIsWebVirtualDir W3SVC/1/Root"
end if
JScript
// 获取参数。
var WshArgs = WScript.Arguments;
if (WshArgs.Length == 3) {
  var machineName = WshArgs(0);
  var adminObjType = WshArgs(1);
  var metabasePath = WshArgs(2);
  var providerObj = GetObject("winmgmts://" + machineName + "/root/MicrosoftIISv2");
  var IIsWebVirtualDirObj = providerObj.get(adminObjType + "='" + metabasePath + "'");
  var IIsWebVirtualDirSettingObj = providerObj.get(adminObjType + "Setting='" + metabasePath + "'");
  WScript.Echo("Read only properties of " + metabasePath + ":");
  var e = new Enumerator(IIsWebVirtualDirObj.Properties_);
  for(; ! e.atEnd(); e.moveNext()) {
  var Property =  
     e.item();
  WScript.Echo(Property.Name
    + " = "
    + Property.Value); }  WScript.Echo(); WScript.Echo("Read/Write properties
  of
  "
  + metabasePath + ":");
  var e = new Enumerator(IIsWebVirtualDirSettingObj.Properties_);
  for(; ! e.atEnd(); e.moveNext())
  {
    var Property = e.item();
    WScript.Echo(Property.Name + " = " + Property.Value);
  }
} // end of if (WshArgs.Length == 3)
else {
  WScript.Echo("Usage:");
  WScript.Echo("cscript /nologo " + WScript.ScriptName + " <computer name> <admin object type> <metabase path>");
  WScript.Echo("Example:");
  WScript.Echo("cscript /nologo " + WScript.ScriptName + " MyMachine IIsWebVirtualDir W3SVC/1/Root");
}
相关主题

© 1997-2003 Microsoft Corporation。保留所有权利