The purpose of this script is to programmatically discover the operating system's version number. In production scripts, this version number becomes the lynch pin, which gives users different mappings depending on whether they are running XP or Window 9x. As ever, the computer thinks 'number' rather than 'text', as a result, the WMI property called version, displays 5.1 rather than XP. No matter, we scripter's can translate numbers into text. This is how I made the translation: If version number = 5.1 then a variable called OSystem = "XP". However, in this example I am going to use Select Case instead of 'If...Then, Else, Endif '
Instructions
- Copy and paste the script below into notepad or OnScript
- Save the file with .vbs extension e.g. OSVer.vbs.
- Double click and examine the message boxes.
-
' OSVer.vbs
' Purpose VBScript to discover the operating system version.
' Learning Points: Win32_ WMI objects. Case Select
' Usage if want to 'branch' depending on the OS
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.2 - April 2007
' --------------------------------------------------------------'
Option Explicit
Dim objWMI, objItem, colItems
Dim strComputer, VerOS, VerBig, Ver9x, Version9x, OS, OSystem
' Here is where we interrogate the Operating System
' On Error Resume Next
' Get the computer name dot = this computer.
strComputer = "."
' This is where WMI interrogates the operating system
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
' Here we filter Version from the dozens of properties
For Each objItem in colItems
VerBig = Left(objItem.Version,3)
Next
' Spot VerBig variable in previous section
' Note the output variable is called OSystem
Select Case VerBig
Case "6.0" OSystem = "Vista"
Case "5.2" OSystem = "Windows 2003"
Case "5.1" OSystem = "XP"
Case "5.0" OSystem = "W2K"
Case "4.0" OSystem = "NT 4.0**"
Case Else OSystem = "Unknown - probably Win 9x"
End Select
Wscript.Echo "Version No : " & VerBig & vbCr _
& "OS System : " & OSystem
WScript.Quit
' End of script