在 VBScript 中,如何检索 InstancesOf 集合的第一个元素?
In VBScript, how can I retrieve the first element of an InstancesOf collection?
我正在编写一个 VBScript 来识别 OS 细节。我在这里使用 InstancesOf Win32_Operating 系统找到了一个示例,但我不想使用示例中的 foreach 循环,而是只想解决第一次出现的问题,所以我做了:
Set SystemSet = GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem")
Set System = SystemSet.Item(0)
也尝试过 Set System = SystemSet(0)
,但每次我都会收到一般性失败错误消息(法语为 Echec générique)。
我怎样才能做到这一点,然后我才能比较 System.Caption
字符串?
GetObject("winmgmts:")
returns一个SWbemServices
对象。根据 SWbemServices
对象的文档 InstanceOf()
方法:
From SWbemServices.InstancesOf
method
creates an enumerator that returns the instances of a specified class according to the user-specified selection criteria.
枚举器的想法是枚举一组对象,这有助于 VBScript For Each
statement 迭代枚举器。
一个简单的例子是;
Dim swbemInstances, swbemInstance
Set swbemInstances = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
For Each swbemInstance In swbemInstances
WScript.Echo swbemInstance.Caption
Next
您可以使用 ItemIndex
方法直接从枚举器访问实例,如文档所述;
From SWbemObjectSet.ItemIndex
method
returns the SWbemObject
associated with the specified index into the collection. The index indicates the position of the element within the collection. Collection numbering starts at zero.
Note: Interesting point the documentation actually cites the Win32_OperatingSystem
class as an example where you likely want to retrieve only one instance and explains how to use ItemIndex
to facilitate it.
From SWbemObjectSet.ItemIndex
method - Examples
Only one instance of Win32_OperatingSystem exists for each operating system installation. Creating the GetObject path to obtain the single instance is awkward so scripts normally enumerate Win32_OperatingSystem even though only one instance is available. The following VBScript code example shows how to use the ItemIndex method to get to the one Win32_OperatingSystem without using a For Each loop.
有点像;
Dim swbemInstance
Set swbemInstance = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem").ItemIndex(0)
WScript.Echo swbemInstance.Caption
中也提到
我正在编写一个 VBScript 来识别 OS 细节。我在这里使用 InstancesOf Win32_Operating 系统找到了一个示例,但我不想使用示例中的 foreach 循环,而是只想解决第一次出现的问题,所以我做了:
Set SystemSet = GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem")
Set System = SystemSet.Item(0)
也尝试过 Set System = SystemSet(0)
,但每次我都会收到一般性失败错误消息(法语为 Echec générique)。
我怎样才能做到这一点,然后我才能比较 System.Caption
字符串?
GetObject("winmgmts:")
returns一个SWbemServices
对象。根据 SWbemServices
对象的文档 InstanceOf()
方法:
From
SWbemServices.InstancesOf
method
creates an enumerator that returns the instances of a specified class according to the user-specified selection criteria.
枚举器的想法是枚举一组对象,这有助于 VBScript For Each
statement 迭代枚举器。
一个简单的例子是;
Dim swbemInstances, swbemInstance
Set swbemInstances = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
For Each swbemInstance In swbemInstances
WScript.Echo swbemInstance.Caption
Next
您可以使用 ItemIndex
方法直接从枚举器访问实例,如文档所述;
From
SWbemObjectSet.ItemIndex
method
returns theSWbemObject
associated with the specified index into the collection. The index indicates the position of the element within the collection. Collection numbering starts at zero.
Note: Interesting point the documentation actually cites the
Win32_OperatingSystem
class as an example where you likely want to retrieve only one instance and explains how to useItemIndex
to facilitate it.From
SWbemObjectSet.ItemIndex
method - Examples
Only one instance of Win32_OperatingSystem exists for each operating system installation. Creating the GetObject path to obtain the single instance is awkward so scripts normally enumerate Win32_OperatingSystem even though only one instance is available. The following VBScript code example shows how to use the ItemIndex method to get to the one Win32_OperatingSystem without using a For Each loop.
有点像;
Dim swbemInstance
Set swbemInstance = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem").ItemIndex(0)
WScript.Echo swbemInstance.Caption