gpt4 book ai didi

com - 可以在 VBScript 中按名称访问 WMI 对象的属性吗?

转载 作者:行者123 更新时间:2023-12-04 14:35:06 25 4
gpt4 key购买 nike

而不是这样的代码:

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

最佳答案

您可以通过 Properties_ 访问 WMI 对象的命名属性。属性(property):

objItem.Properties_("Age")
objItem.Properties_.Item("Age")

当然,您也可以枚举 Properties_收藏:
For Each objItem in colItems
For Each prop in objItem.Properties_
If IsArray(prop) Then
WScript.Echo prop.Name & ": " & Join(prop, ", ")
Else
Wscript.Echo prop.Name & ": " & prop
''# -- or --
''# Wscript.Echo prop.Name & ": " & prop.Value
End If
Next
Next

关于com - 可以在 VBScript 中按名称访问 WMI 对象的属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4252430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com