PowerShell Ezine, Logon Scripts

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.

ˆ

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 & _


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.

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

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

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

Please report a broken link, or an error.