可以在 VBScript 中按名称访问 WMI 对象的属性吗?
而不是这样的代码:
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_IP4RouteTable",,48)
For Each objItem in colItems
Wscript.Echo "Age: " & objItem.Age
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "Description: " & objItem.Description
Next
是否可以通过名称访问每个属性,例如以下语法之一:
Wscript.Echo "Age: " & objItem("Age")
Wscript.Echo "Age: " & objItem.Properties("Age")
Wscript.Echo "Age: " & objItem.Item("Age")
更好的是,有什么方法可以执行以下操作:
Dim colItems
Dim objItem
Dim aProperty
Set colItems = objWMIService.ExecQuery("Select * from Win32_IP4RouteTable",,48)
For Each objItem in colItems
For Each aProperty in objItem.Properties
Wscript.Echo aProperty.Name & ": " & objItem(aProperty.Name)
Next
Next
Instead of code like this:
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_IP4RouteTable",,48)
For Each objItem in colItems
Wscript.Echo "Age: " & objItem.Age
Wscript.Echo "Caption: " & objItem.Caption
Wscript.Echo "Description: " & objItem.Description
Next
Is it possible to access each property by name, something like one of these syntaxes:
Wscript.Echo "Age: " & objItem("Age")
Wscript.Echo "Age: " & objItem.Properties("Age")
Wscript.Echo "Age: " & objItem.Item("Age")
And even better, is there any way you can do something like:
Dim colItems
Dim objItem
Dim aProperty
Set colItems = objWMIService.ExecQuery("Select * from Win32_IP4RouteTable",,48)
For Each objItem in colItems
For Each aProperty in objItem.Properties
Wscript.Echo aProperty.Name & ": " & objItem(aProperty.Name)
Next
Next
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过
Properties_
< 访问 WMI 对象的命名属性/a> property:当然,你也可以枚举
Properties_
集合:You can access named properties of WMI objects via the
Properties_
property:And of course, you can also enumerate the
Properties_
collection: