Guy’s Scripting Ezine 19 – Disks with WMI and VbTab

Guy’s Scripting Ezine 19 – Scripting Disks with WMI and VbTab

 ♣

This Week’s Secret

When I was a beginner at VBScript, if I wanted to create a line break in message box, then I used: Chr(13), whereas now I prefer: VBCr. Once I discovered VBCr there was no holding me. VBCr led me on VbTab which I employ to align output messages.

When ever possible, I love to kill two birds with one stone. So, while I employ WMI methods to expose properties of the disk, I also use VbTab commands to format the message box output. The result is that I can show you how to both interrogate the disk, and master the VBScript syntax. WMI takes the covers of the operating system, so that we can see what’s ‘under the bonnet’. This week we will examine the Disk Partition and also the characteristics of the very disk drive hardware.

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 display information about the volumes (partitions) on your hard drive.

The purpose of this script is to display the properties of your disk’s volumes. In particular, the script will discover which volume is bootable.

From a technical point of view, note that the underlying object = winmgmts. This exposes the WMI so that we can interrogate one of the many collections of objects. In this example we persuade WMI to display information about the Win32_DiskPartition.

Instructions

  1. Copy and paste the script below into notepad. Save the file with .vbs extension e.g. volume.vbs
  2. Double click and observer the message box.

‘ VBScript to display Volume (partition) information
‘ Guy Thomas February 2004.
‘ ******************************
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colDiskPartitions = objWMIService.ExecQuery _
(“Select * from Win32_DiskPartition”)
For each objPartition in colDiskPartitions
WScript.Echo “Bootable: ” & VBTab & objPartition.Bootable & VBCr & _
“Boot Partition: ” & VBTab & objPartition.BootPartition & VBCr & _
“Description: ” & VBTab & objPartition.Description & VBCr & _
“Device ID: ” & VBTab & objPartition.DeviceID & VBCr & _
“Disk Index: ” & VBTab & objPartition.DiskIndex & VBCr & _
“Index: ” & VBTab & VBTab & objPartition.Index & VBCr & _
“Size: ” & VBTab & VBTab & objPartition.Size & VBCr & _
“Type: ” & VBTab & VBTab & objPartition.Type
Next
‘ End of example VBScript

Learning points

Note 1: There is only one WScript statement and consequently only one message box.  However, the message box displays many lines thanks to these two punctuation marks: & (ampersand) and _ (underscore).

Last week we experimented with VBCr, if you remember its purpose was to create a line break in a message box. This week I am going to concentrate on the sister command VBTab which tabulates the output and so makes it easier to read the ten or so rows in the message box. Perhaps the best way to see the power of VBTab is by comparing Example 1a (above), with Example 1b below.

Example 1b Display information about the volume – poor presentation.

Here is a script which lacks the vital & VBCr. While the script runs perfectly, it is the output that looks a mess and needs our attention. The script even has VbTab, but the display still lacks perfection.

‘ VBScript to display Volume information (Poor Formatting)
‘ Guy Thomas February 2004.
‘ ******************************
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colDiskPartitions = objWMIService.ExecQuery _
(“Select * from Win32_DiskPartition”)
For each objPartition in colDiskPartitions
WScript.Echo “Bootable: ” & VBTab & objPartition.Bootable & _
“Boot Partition: ” & VBTab & objPartition.BootPartition & _
“Description: ” & VBTab & objPartition.Description & _
“Device ID: ” & VBTab & objPartition.DeviceID & _
“Disk Index: ” & VBTab & objPartition.DiskIndex & _
“Index: ” & VBTab & VBTab & objPartition.Index & _
“Size: ” & VBTab & VBTab & objPartition.Size & _
“Type: ” & VBTab & VBTab & objPartition.Type
Next
‘ End of example VBScript

Learning points

A theme I am going to introduce here and reinforce later,  is the idea of giving you scripts with deliberate errors and challenging you to correct them.

Guy’s Challenge: search in notepad for: & _ then replace with: & VBCr & _

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 2a – Display information about your hard disks capacity and geometry.

The purpose of this script is to extract information about how many disks your computer has, their capacity and the geometry of their tracks, sectors and cylinders. Spot the difference in the Select * from Win32_DiskDrive compared with Win32_DiskPartition in Example 1.

‘ VBScript to to display disk facts and figures
‘ Guy Thomas February 2004.
‘ ******************************
On Error Resume Next
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_DiskDrive”,,48)
For Each objItem in colItems
objItem.size = objItem.size /1000000000
WSCript.echo “Hostname:” & VBTab & objItem.SystemName & VBcr & _
“Disk Type: “& VBTab & objItem.InterfaceType & VBcr & _
“Model: “& VBTab & VBTab & objItem.manufacturer & VBcr & _
“Media Type: ” & VBTab & objItem.MediaType & VBcr & _
“Disk Number :” & VBTab & objItem.deviceID & VBcr & _
“Disk Size: “& VBTab & objItem.size & ” MB” & VBcr & _
“BytesPerSector: ” & VBTab & objItem.BytesPerSector & VBcr & _
“TotalCylinders: “& VBTab & objItem.TotalCylinders & VBcr & _
“TotalHeads: “& VBTab & objItem.TotalHeads & VBcr & _
“TotalSectors: “& VBTab & objItem.TotalSectors & VBcr & _
“TotalTracks: “& VBTab & objItem.TotalTracks & VBcr & _
“TracksPerCylinder: “& VBTab & objItem.TracksPerCylinder
Next
‘ End of example VBScript

Learning points

Note 1: “Disk Size: “& VBTab & objItem.size & ” MB” & VBcr & _
objItem.size returns the disk size in bytes. To make the value more meaningful can you see how I divided by 1 x 9 zeros and so converted the output in to Megabytes.

Note 2: A tiny touch, of no great consequence, is adding the ” MB”. Perhaps you see the need for the space after the first speech mark.

Note 3: This line needs 5 & (ampersands) to concatenate the command.  Be sure to check you own scripts if you get an error: Expected end of statement Code: 800A0401 error.  Remove one of the & to see what I mean, and generate the error.

Example 2b – Display information about your hard disks.

Developing my theme of deliberate errors, I have a challenge for you – fix a problem in the script below. When you create your vbs file from the text below you will get a ‘deliberate’ error message. See if you can troubleshoot the problem.

‘ VBScript to to display disk facts and figures
‘ Guy Thomas February 2004.
‘ ******************************
On Error Resume Next
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_DiskDrive”,,48)
For Each objItem in colItems
objItem.size = objItem.size /1000000000
WSCript.echo “Hostname:” & VBTab & objItem.SystemName & VBcr & _
“Disk Type: “& VBTab & objItem.InterfaceType & VBcr & _
“Model: “& VBTab & VBTab & objItem.manufacturer & VBcr & _
“Media Type: ” & VBTab & objItem.MediaType & VBcr & _
“Disk Number :” & VBTab & objItem.deviceID
“Disk Size: “& VBTab & objItem.size & ” MB” & VBcr & _
“BytesPerSector: ” & VBTab & objItem.BytesPerSector & VBcr & _
“TotalCylinders: “& VBTab & objItem.TotalCylinders & VBcr & _
“TotalHeads: “& VBTab & objItem.TotalHeads & VBcr & _
“TotalSectors: “& VBTab & objItem.TotalSectors & VBcr & _
“TotalTracks: “& VBTab & objItem.TotalTracks & VBcr & _
“TracksPerCylinder: “& VBTab & objItem.TracksPerCylinder
Next
‘ End of example VBScript

Learning Point

The above script should give a WSH error code 800A0400 – Expected statement on Line 13 Char 5.

See if you can correct the script your self.

Here is the problem line followed by the correction.

"Disk Number :" & VBTab & objItem.deviceID
"Disk Number :" & VBTab & objItem.deviceID & VBcr & _

Summary

The main technical factor in this week’s scripts is exploring WMI.  We use the disk as a vehicle to test our script.  The previous Ezine dealt with carriage returns, and that formatting theme is extended by the introduction of VBTab and VBcr.

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