Search code examples
vbscriptwmi

Getting multiple values from remote computers using vbscript


Working around no PowerShell in an environment by using vbScript (which I haven't touched in years now).

Trying to get info using the following:

    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

I can get everything except the OS (from oss):

script.vbs(8, 5) Microsoft VBScript runtime error: Wrong number of arguments or invalid property assignment

Any help would be appreciated.


Solution

  • Change the query to:

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

    Now that you have got a collection of all the OS installed, you can use the caption property and store then in an array(in case, there are multiple OS)

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

    Now the array contains the operating system version of all the operating systems. If you have just one OS, you can use the 0th element of this array as shown below:

    Wscript.Echo arrCap(0)
    

    Your Final Code should look like:

    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
    

    Here is the output:

    enter image description here