Guy’s Scripting Ezine 41 – Case Select

Contents for Guy’s Scripting Ezine 41 – Case Select


This Week’s Secret

20 years ago I gave up playing ‘Shoot em up’ games.  These days I get the same kick from ‘playing’ with VBScript.  This week’s indulgence is Case Select statements.

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 Case Select Disks

Scenario: Which drives are in use?  What sort of disk corresponds to each drive letter?  Is it a hard disk, network drive or floppy drive?

‘ SelectCaseDrive.vbs
‘ Version 1.2
‘ Guy Thomas 15th August 2004

Option Explicit

Dim objWMIService, colDisks, objDisk ‘ Objects
Dim strDriveType, strComputer ‘ Strings

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
Select Case objDisk.DriveType
Case 1 strDriveType = "Drive could not be determined."
Case 2 strDriveType = "Removable Drive (Floppy?)"
Case 3 strDriveType = "Local hard disk."
Case 4 strDriveType = "Network disk."
Case 5 strDriveType = "Compact disk (CD)"
Case 6 strDriveType = "RAM disk."
Case Else strDriveType = "Drive type Problem."
End Select
Wscript.Echo "Device ID = " & objDisk.DeviceID &vbCr _
& "Drive Type : " & strDriveType

Learning Points

Note 1:  The script relies on a For Each….. Next loop.

Note 2: The featured command is:  Select Case…. End Select.   Inside the statement are your series of values, Case 1 =, Case 2= etc.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Orion performance monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 2 Case Select Printers

Scenario: You wish to check the name and the ‘Status’ of your printers.


‘ SelectCase.vbs
‘ Version 1.4
‘ Guy Thomas 15th August 2004

Option Explicit

Dim objWMIService, colInstalledPrinters, objPrinter’ Objects
Dim strPrinterStatus, strComputer ‘ Strings

‘ ——————————————–
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
‘ Wscript.Echo "Name: " & objPrinter.Name
‘ Wscript.Echo "Location: " & objPrinter.Location
Select Case objPrinter.PrinterStatus
Case 1 strPrinterStatus = " Other"
Case 2 strPrinterStatus = " Unknown"
Case 3 strPrinterStatus = " Idle"
Case 4 strPrinterStatus = " Printing"
Case 5 strPrinterStatus = " Warmup"
Case Else strPrinterStatus = " No number"
End Select
Wscript.Echo "Share Name : " & objPrinter.ShareName & vbCr _
& "Server Name : " & objPrinter.ServerName & vbCr _
& "Location : " & objPrinter.Location & vbCr _
& "Printer Status : " & objPrinter.PrinterStatus & strPrinterStatus


‘Case Select’ is a useful technique where you have a list of possible outcomes.  Admittedly, you could use multiple ‘Else If’ statements, but Case Select is a more elegant solution.

WMI is the way of the future, never waste a chance to learn how VBScript can quiz the operating system.  Variables offer great techniques for controlling scripts.  For example, it is useful to control a value from a central location, usually at the start of the script.

See more about VBScript techniques

VBScripts  • WMI  • Ezines  • Logon Scripts  • Tool Kit  •SLA Monitor  • Ezine 26 Msg Box

Ezine 41 VBS Select case  • Ezine 46 MsgBox  • Free Response Time Tool

Ezine 55 VBS Select case  • Ezine 61 Objects methods   •Ezine 70 GetEx Split