Search code examples
vbscriptwmic

Using WMI objects in VB script to decide which programs to run


I have a vbscript that will find the OS, Service pack and OS Architecture but when i try to use the object in a If then statement its not defined, I tried to declare it but that didn't work. What i am trying to complete is create a script that will define the OS, Service pack and whether the system is x86 or x64 and the execute a program that will install the updates specific for that system but I'm stuck.

Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

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

For Each objOperatingSystem in colOperatingSystems
    Wscript.Echo "Version: " & objOperatingSystem.Version
    Wscript.Echo "Service Pack: " & objOperatingSystem.CSDVersion
    Wscript.Echo "CPU Bit: " & objOperatingSystem.OSArchitecture
Next

Solution

  • I am assuming you are not storing those variables you are echoing with "wscript.echo"..

    You could use this example (or make a case statement):

    Set dtmConvertedDate = CreateObject("WbemScripting.SWbemDateTime")
    
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
    Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
    
    For Each objOperatingSystem in colOperatingSystems
       osVersion      = objOperatingSystem.Version
       osCSDVersion   = objOperatingSystem.CSDVersion
       osArchitecture = objOperatingSystem.OSArchitecture
    Next
    
    Wscript.Echo "Version: "  & osVersion
    Wscript.Echo "Service Pack: " & osCSDVersion
    Wscript.Echo "CPU Bit: " & osArchitecture
    
    
    If InStr(LCase(osArchitecture), "64-bit") Then
        wscript.echo "64-bit"
        ' .... Do stuff here ....
    Else
        wscript.echo "not 64-bit"
        ' .... Do stuff here ....
    End If