勇芳软件工作室.汉化:  Windows Management Instrumentation > Scripting API for WMI > Scripting API Objects > SWbemObjectSet >

ItemIndex Method

Previous pageReturn to chapter overviewNext page

描述

 

ItemIndex方法将与指定索引关联的SWbemObject返回到集合中。索引表示集合中元素的位置。集合编号从零开始。

 

PowerBASIC 语法

 

PROPERTY GET ItemIndex ( _

BYVAL lIndex AS LONG _

) AS ISWbemObject

 

参数

 

lIndex

 

LONG.集合中项目的索引。

 

返回值

 

如果成功,则返回所请求的SWbemObject对象。

 

OBJRESULT

 

错误(名称/十进制/十六进制)

含义

wbemErrFailed

-2147217407

&H80041001

未指定错误

wbemErrInvalidParameter

-2147217400

&H80041008

指定了无效参数。如果提供了负数索引号,则返回该错误。

wbemErrOutOfMemory

-2147217402

&H80041006

没有足够的内存来完成操作。

wbemErrNotFound

-2147217406

&H80041002

未找到请求的项目。

 

示例代码

 

以下代码示例输出显示与Win32_OperatingSystem关联的实例。

 

Windows Server 2008

  |C:\Windows|\Device\Harddisk0\Partition1

Win32_ComputerSystem.Name="Test1"

Win32_AutochkSetting.SettingID="Windows Server 2008

  |C:\\Windows|\\Device\\Harddisk0\\Partition1"

 

示例代码[VBScript]

 

所有Win32_Process实例的集合的以下VBScript代码示例查询将显示前三个进程的名称。

 

strComputer =“。”

设置objWMIService = GetObject(“winmgmts:\\\\”&_

 strComputer &“\\ root \\ cimv2”)

 

设置colProcesses = _

 objWMIService.Execquery(“从Win32_Process中选择*”)

wscript.Echo  colProcesses.ItemIndex(0).Name

Wscript.Echo  colProcesses.ItemIndex(1).Name

Wscript.Echo  colProcesses.ItemIndex(2).Name

 

示例代码[VBScript]

 

每个操作系统安装仅存在一个Win32_OperatingSystem实例。创建GetObject路径以获取单个实例是尴尬的,所以脚本通常枚举Win32_OperatingSystem即使只有一个实例可用。以下VBScript代码示例显示如何使用ItemIndex方法获取到不使用对于每个循环的Win32_OperatingSystem.

 

strComputer =“。”

设置objWMIService = GetObject(“winmgmts:”_

 &“{impersonationLevel = impersonate}!\\\\”&strComputer &“\\ root \\ cimv2”)

 

设置colOperatingSystems = objWMIService.ExecQuery _

 (“从Win32_OperatingSystem选择*”)

 

Wscript.Echo“Caption:”&colOperatingSystems.ItemIndex(0).Caption

 

示例代码[VBScript]

 

以下VBScript代码示例获取与Win32_OperatingSystem关联的实例,例如Win32_SystemOperatingSystem.

 

strComputer =“。”

设置objWMIService = GetObject(“winmgmts:\\\\”&_

 strComputer &“\\ root \\ cimv2”)

 

设置colOS = _

 objWMIService.Execquery(“从Win32_OperatingSystem选择*”)

 Wscript.Echo  colOS.ItemIndex(0).Name

 

集colAssociators = colOS.ItemIndex(0).Associators_

 对于colAssociators中的每个Associator     

   Wscript.Echo Associator.Path_.RelPath         

 接下来

 

备注

 

ItemIndex方法允许WMI客户端使用任何语言编写的脚本和应用程序是统一的方式来操作数组的集合。此方法可与SWbemObjectSet集合一起使用。查询,例如SWbemServices.AssociatorsOfSWbemServices.ReferencesToSWbemServices.InstancesOfSWbemServices.ExecQuery返回SWbemObjectSet集合。SWbemNamedValueSetSWbemPrivilegeSetSWbemPropertySetSWbemQualifierSet中的SWbemObjects集合不支持ItemIndex方法。

 

ItemIndex也可以用于获取单例类的单个实例。

 

要求

 

客户

需要Windows Vista,Windows XP,Windows 2000 Professional,Windows NT Workstation 4.0 SP4及更高版本,Windows Me,Windows 98或Windows 95 OSR2及更高版本。

服务器

需要Windows Server 2008,Windows Server 2003,Windows 2000 Server或Windows NT Server 4.0 SP4及更高版本。

类型库

使用Wbemdisp.tlb。

Wbemdisp.inc。

DLL

需要Wbemdisp.dll。