Search code examples
cmdvbscriptwmic

Why am I getting different results between CMD and VBS?


When I issue the following command in a CMD prompt, the correct value is returned for the AdapterRAM object:

WMIC Path Win32_VideoController Get AdapterRAM

The correct value of 8589934592 is returned. However when I utilize VBScript with the same query, an incorrect value is returned for the same AdapterRam object:

sPC = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sPC & "\root\cimv2")
Set cSettings = objWMIService.ExecQuery("Select AdapterRAM From Win32_VideoController Where DeviceID='VideoController1'")
For Each oPC in cSettings
    GPURAMSize = oPC.AdapterRAM
    WScript.Echo GPURAMSize
Next

The incorrect value of -1048576 is returned for the same AdapterRAM object.

Am I missing something here? Why the difference in values returned between utilizing the same query from a CMD prompt versus a VBScript?

Thanks in advance.


Solution

  • Thanx to all the help from everyone that has contributed comments, but most especially user 692942 for providing the link that helped me solve the problem. Here's what I came up with...

    strComputer = "."
    Dim VideoController, PosValueBegin, PosValueEnd, Length, StringToFind
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set cSetting = objWMIService.ExecQuery("Select Name, Manufacturer, Model, SystemSKUNumber, TotalPhysicalMemory From Win32_ComputerSystem")
    mathConversion = 1024 * 1024 * 1024
    Set cSetting = objWMIService.ExecQuery("Select DeviceID, Caption, AdapterRAM From Win32_VideoController Where DeviceID='VideoController1'")
    For Each objComputer in cSetting
        GPUID = objComputer.Caption
    Next
    For Each objComputer in cSetting
        StringToFind = "AdapterRAM = "
        VideoController = objComputer.GetObjectText_(0)
        PosValueBegin = InStr(VideoController, StringToFind) + Len(StringToFind)
        PosValueEnd = InStr(PosValueBegin, VideoController, ";")
        Length = PosValueEnd - PosValueBegin
        ActualNumber = Mid(VideoController, PosValueBegin, Length)
        GPURAMSize = RoundUp(ActualNumber / mathConversion)
    Next
    wScript.Echo "GPU:  " & GPUID & " (" & GPURAMSize & "GB)"
    wScript.Quit