How can I test whether or not “Microsoft Windows 7 Home Premium” is the operating system using VBScript?

StackOverflow https://stackoverflow.com/questions/2121439

Question

My first attempt is to query Win32_OperatingSystem for the caption, and test whether the caption "equals" the operating system I am testing for:

Dim objWMIService, strComputer
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")


msgbox getOperatingSystemCaption()
msgbox meetsOperatingSystemRequirement()



Function getOperatingSystemCaption()

     Dim strCaption, colOperatingSystems, objOperatingSystem

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

     For Each objOperatingSystem in colOperatingSystems
        strCaption = objOperatingSystem.Caption
        Exit For 
     Next

    getOperatingSystemCaption = strCaption

End Function





Function meetsOperatingSystemRequirement()

    meetsOperatingSystemRequirement = False 

    If getOperatingSystemCaption() = "Microsoft Windows 7 Home Premium" Then 

       meetsOperatingSystemRequirement = True

    End If 


End Function

I suppose I can use InStr, however I still do not understand why the "Caption" and my string are not equal.

Was it helpful?

Solution

Are you sure you have "Microsoft Windows XP" and not "Microsoft Windows XP Professional" ?. If you use "=" sign, then you will not catch it because it expects to match exact string. Use instr() would be better if you want partial match. Otherwise, add in "Professional"

You can put in some debugging after caption is found

....
        msgbox strCaption & " " & len(strCaption)
        getOperatingSystemCaption = strCaption
....

and try a different way

.....
    myCaption = getOperatingSystemCaption()
     msgbox myCaption & " " & len(myCaption)
    If myCaption = "Microsoft Windows XP Premium Home" Then 
......

check the length as well...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top