Guy’s Scripting Ezine 52 – OS Version Number

Contents for Guy’s Scripting Ezine 52 – OS Version Number

This Week’s Secret

Thanks to people emailing me with feedback, I am building up a picture of what you the reader wants.  The only trouble is that different people want different scripts.  Thus, my personal mission is to design scripts that work at two or even three levels.

My first and most important goal, is to produce a script which solves a particular problem, for example, this week’s script extracts the operating system version number.  We can then use this version number to branch a script.  This is what I mean by ‘branch a script’: if version number = 5.1 (XP) the apply Group Policy abc, else apply Group Policy xyz.

My secondary goal is for those who want to learn more about VBScript; for them I add Learning Points at the end of each script.  My OSVer.vbs script highlights the Select Case construction.  Master the ‘Case’ statements here, then apply Select Case to other scripts.

My third goal is one of my hobby horses, I want to promote the concept of building up scripts gradually.  I urge you to break up real scripts into sections.  Only when each section works perfectly, bolt them together and so create the complete package.  Here I speak from the heart, because the majority of scripts that I am asked to troubleshoot are so long and complex that it’s a nightmare trying to debug the errors.  What I see is people who keep adding more section to a script until failure is inevitable.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Example 1 – To Script the operating system version number

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

  1. Copy and paste the script below into notepad or OnScript
  2. Save the file with .vbs extension e.g. OSVer.vbs.
  3. 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 https://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

WMI Learning Points

Note 1:  WMI’s Version is a property of the Win32_OperatingSystem class.  To tell the truth Version does not report Windows 9x operating system numbers.  From a pure scripting point of view this not a problem as I catch Version numbers like 95 with the Case Else… statement. There is a rumour that the Win 9x attribute is called Version9x not plain Version. 

Note 2:  However I do have a plan B, that is to substitute BuildNumber for Version on line 24, for example objItem.BuildNumber.  Here are the actual build numbers: Windows 95 – 950, NT – 1381, Windows 2000 – 2195, XP – 2600, Windows 2003 – 3790.

VBScript Learning Points

Note 3:  For branching statements, Select Case is more efficient than multiple If, then else, else, else, else, else. Endif.

Note 4:  The insignificant statement: Left(objItem.Version,3) gave me more trouble than any other line.  That was because silly me tried to use Left(objItem.Version),3.   My learning point is that the Left 3 statement gives me just three digits, which is simpler than the raw string with multiple dots.  5.1 rather than 5.1.12.87.41.98.

Note 5: Where next?.   On to Example 2 where we use the version number information to create a branching script.

Example 2 – Branching using operating system version number

 

‘ OSVerBranch.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 https://computerperformance.co.uk/
‘ Version 3.2 – November 14th 2004
‘ ————————————————————–‘
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 "5.0" OSystem = "W2K"
Case "5.1" OSystem = "XP"
Case "5.2" OSystem = "Windows 2003"
Case "4.0" OSystem = "NT 4.0"
Case Else OSystem = "Unknown – probably Win 9x"
End Select

 ‘ Actual Branching section
If OSystem = "XP" Then
    Wscript.Echo " This is where you set XP Group Policy "
    Else If OSystem = "Windows 2003" Then
   Wscript.Echo " No Group Policy on the server"
    Else If OSystem = "W2K" Then
    Wscript.Echo " You could set W2K Group Policy "
    Else Wscript.Echo " Default group policy "
    End If
    End If
End If

WScript.Quit

‘ End of script

Learning Points

Note 1:  This script is purely for learning.  Whilst example 2 is ugly, I hope that you can see how the value that WMI version returns could be used to create a branch or fork later in the script.

Note 2:  The fact that this script is so ugly, makes me hope that you will accept my challenge to amend it.  What you could do is merge the Select Case and ‘ Actual branching section, into one unit.  Moreover, in that unit perhaps you could think of something more useful then WScript.Echo, for example, map network drive.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Summary

Knowing the operating system version number gives your script a handle.  That handle or test, is invaluable when it comes to branching decisions later in the script.  For example if operating system = XP, then provide a special group policy, else just provide the basic group policy.

See More WMI Scripts

• WMI  • Ezines  • WMI Basics   • WMI PowerShell  • Free SolarWinds WMI Monitor

Ezine 9 WMI  • Ezine 10 WMI  • Ezine 19 WMI  • Ezine 40 Variables  • Ezine 48 WMI 

Ezine 52 WMI OS  • Ezine 76 WMI Classes  • Ezine 93 WMI  • Ezine 94 WMI  • Ezine 95 WMI

Ezine 110 WMI PowerShell  •Ezine 114 WMI Path  • Tool Kit