使用vbscript从远程计算机获取多个值

时间:2017-05-22 07:26:44

标签: vbscript wmi

使用vbScript在环境中不使用PowerShell(我现在已经多年没有触及)。

尝试使用以下内容获取信息:

    strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery ("Select * from CIM_Datafile Where Name = 'c:\\windows\\system32\\drivers\\srv.sys'")
Set oss = objWMIService.ExecQuery ("Select caption from Win32_OperatingSystem")
Set wshShell = CreateObject( "WScript.Shell" )
strUserDomain = wshShell.ExpandEnvironmentStrings( "%USERDOMAIN%" )
For Each objFile in colFiles
    Wscript.Echo strComputer & "," & strUserDomain & "," & oss & "," & objFile.Version
Next

我可以获得除OS之外的所有内容(来自oss):

  

script.vbs(8,5)Microsoft VBScript运行时错误:错误的数量   参数或无效的属性赋值

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

将查询更改为:

Set oss = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")

现在你已经安装了所有操作系统的集合,你可以使用caption属性然后存储在一个数组中(如果有多个操作系统)

Dim arrCap()
Dim intCtr: intCtr=-1
For each os in oss
    intCtr=intCtr+1
    Redim preserve arrCap(intCtr)
    arrCap(intCtr) = os.Caption
Next

现在,该阵列包含所有操作系统的操作系统版本。如果您只有一个操作系统,则可以使用此数组的第0个元素,如下所示:

Wscript.Echo arrCap(0)

您的最终代码应如下所示:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery ("Select * from CIM_Datafile Where Name = 'c:\\windows\\system32\\drivers\\srv.sys'")
Set oss = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
Set wshShell = CreateObject( "WScript.Shell" )

Dim arrCap()
Dim intCtr: intCtr=-1
For each os in oss
    intCtr=intCtr+1
    Redim preserve arrCap(intCtr)
    arrCap(intCtr) = os.Caption
Next

strUserDomain = wshShell.ExpandEnvironmentStrings( "%USERDOMAIN%" )
For Each objFile in colFiles
    Wscript.Echo strComputer & "," & strUserDomain & "," & arrCap(0) & "," & objFile.Version
Next

这是输出:

enter image description here