无法从objWMIService.ExecQuery直接访问变量oss.Caption

时间:2015-05-16 04:59:33

标签: vbscript wmi

我只是试图获得操作系统标题,其中包含" Windows 8.1 Enterprise"但我必须使用循环访问它,并在该循环中设置变量strSearchString = os.Caption。为什么我不能strSearchString = oss.Caption或仅参考oss.Caption

示例代码:如果不是Windows 8,则会出错。

Sub Windows8_Enterprise_Check
  'OS Determination
  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
                      & strComputer & "\root\cimv2")
  Set oss = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
  For Each os in oss
    strSearchString = os.Caption
  Next

  strSearchFor1 = "Windows 8"
  strSearchFor2 = "Enterprise"

  If (InStr(1, strSearchString, strSearchFor1) < 1) _
       OR (InStr(1, strSearchString, strSearchFor2) < 1) Then
    'We didn't find strSearchFor in strSearchString
    Wscript.Echo "OS Level Error: " & strSearchString _
      & " is invalid for this script"
    WScript.Quit(49)
  Else
    Wscript.Echo "OS Level Test: PASS"
  End If
End Sub

2 个答案:

答案 0 :(得分:1)

因为查询返回的oss是一个集合。集合的项目/元素具有.Caption属性,但集合没有。

答案 1 :(得分:0)

当您查询WMI时,默认情况下它会返回对象的列表/集合,其中每个对象都包含该类的属性数据。

所以当你说oss时,把它想象成每个对象(在你的情况下是os)的对象数组

Oss是

minify-html

所以当你说os时,它将指向上面列出的一个对象。 现在你可以思考     OS = OSS(0)

其中Oss(0)具有不同的属性,其中一个是标题。 因此,您需要指向单个对象,然后访问内部的属性..

相关问题