Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with objWMIService.ExecQuery and if statement

Status
Not open for further replies.

qtpounder

Technical User
Feb 4, 2010
2
US
I am having a problem with my script. I am a noob when it comes to VBS so please excuse my ignorance. I echo the value of osversion varible and it has my os version in it. But then once it enters into the IF statement it is always false which is should be true because I am running Microsoft Windows 7 Ultimate which is what I am making osversion = to in the if statement.

I am curious what I am doing wrong. Thanks for your help in advanced.

---------------------------------------------

' Original script created by:
' Author Guy Thomas ' Version 1.4 - November 2005
' -------------------------------------------------------'
'Option Explicit
Dim objWMIService, objItem, colItems, osversion
Dim strComputer, strList

strComputer = "."

' WMI Connection to the object in the CIM namespace
Set objWMIService = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")

' WMI Query to the Win32_OperatingSystem
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

' For Each... In Loop (Next at the very end)
For Each objItem in colItems
osversion = objItem.Caption
WScript.Echo osversion
If osversion = "Microsoft Windows 7 Ultimate" Then
WScript.Echo "It worked!"
Else
WScript.Echo "It did not work"

End If

Next

WSCript.Quit

--------------------------------------------------
 
What is the very exact result of the following ?
Code:
WScript.Echo "'" & osversion & "'"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for the reply. I found the problem. I had to add < to = line. So it looks like this:

If osversion <= "Microsoft Windows 7 Ultimate" Then
WScript.Echo "It worked!"
Else
WScript.Echo "It did not work"
WScript.Echo osversion
End If


==========================================================
The full working script is this:
==========================================================
' Original script created by:
' Author Guy Thomas ' Version 1.4 - November 2005
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objItem, colItems, osversion
Dim strComputer, strList

strComputer = "."

' WMI Connection to the object in the CIM namespace
Set objWMIService = GetObject("winmgmts:\\" _
& strComputer & "\root\cimv2")

' WMI Query to the Win32_OperatingSystem
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

' For Each... In Loop (Next at the very end)
For Each objItem in colItems
osversion = objItem.Caption
WScript.Echo osversion
If osversion <= "Microsoft Windows 7 Ultimate" Then
WScript.Echo "It worked!"
Else
WScript.Echo "It did not work"
WScript.Echo osversion
End If

Next
WSCript.Quit

===============================================


Thanks for your quick response! Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top