PowerShell Ezine, Logon Scripts

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.

ˆ

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
Next
 

 

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.

ˇ

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
Next

 

Summary

'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.

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


 *


Google

Webcomputerperformance.co.uk

GFi Events Manager

Guy Recommends: GFi EventsManager

Here is a solution to monitor, manage and archive thousands of events that are generated by devices across your entire network.  Get your free evaluation copy of GFI EventsManager.

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.