Introduction to WMI Disks
Most WMI scripts obtain information that is available by launching various GUIs. In the case of disks, we could launch the Disk
Administrator and observe the partition information in the window. However, the great advantage of script is that we can filter the data, interrogate multiple machines, and output the results to screen or
to a text
file. Perhaps the most important part of scripting is, building in logic and taking action based on information received. For example, if Manufacturer Model = xyz then apply hotfix q123. There are
two types of WMI disk object: Win32_DiskDrive - Physical Disk data. Based on the number of blocks, tracks or sectors. (Pure disk data. See more on this page.) Win32_LogicalDisk - Partition size Disk Type and FreeSpace.
See more here. This page has an important extra scripting feature, how to output WMI information to a text file. Whilst my script explains how to write disk data to a file, you could adapt the
method to many other WMI scripts.
Topics for WMI - Disks
♣
This WMI sample file is about displaying low level disk information. How big is the whole disk, not just the partitions? What are the block, sector
and track sizes? Which SCSI Bus is attached to which physical disk? Who is the manufacturer? What signature did the operating system give the disk? Prerequisites for your Physical
Disk Information WMI Script No prerequisites. Be careful with _ Underscores when you copy and paste.
Instructions for your Physical Disk Information WMI Script
- Copy and paste the example script below into notepad or a VBScript editor.
- Decide which machine on your network to interrogate and then change line 10:
strComputer = "." to the name of that machine. The default "." means the local machine.
- Save the file with a .vbs extension, for example: DiskDriveLogical.vbs
- Double click DiskDriveLogical.vbs and check the properties of each partition.
Script to Interrogate Logical Disk
' DiskDriveLogical.vbs ' Sample VBScript to interrogate a physical disk with WMI ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 2.3 - November 2005 '
--------------------------------------------------------------' Option Explicit Dim objWMIService, objItem, colItems, strComputer
' On Error Resume Next strComputer = "."
Set
objWMIService = GetObject("winmgmts:\\" _ & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery(_ "Select * from Win32_DiskDrive")
For Each objItem in colItems Wscript.Echo
"Computer: " & objItem.SystemName & VbCr & _ "Status: " & objItem.Status & VbCr & _ " ==================================" & VbCr & _ "Name: " & objItem.Name & VbCr & _ "Description: " &
objItem.Description & VbCr & _ "Signature: " & objItem.Signature & VbCr & _ "Manufacturer: " & objItem.Manufacturer & VbCr & _ "Model: " & objItem.Model & VbCr & _ "Size: " & Int(objItem.Size
/(1073741824)) & " GB" & VbCr & _ "Number of Partitions: " & objItem.Partitions & VbCr & _ "Total Cylinders: " & objItem.TotalCylinders & VbCr & _ "Tracks PerCylinder: " & objItem.TracksPerCylinder
& VbCr & _ "Total Heads: " & objItem.TotalHeads & VbCr & _ "Total Sectors: " & objItem.TotalSectors & VbCr & _ "Bytes PerSector: " & objItem.BytesPerSector & VbCr & _ "Sectors PerTrack: " &
objItem.SectorsPerTrack & VbCr & _ "Total Tracks: " & objItem.TotalTracks & VbCr & _ " -------- SCSI Info ---------- "& VbCr & _ "SCSI TargetId: " & objItem.SCSITargetId & VbCr & _ "SCSI Bus: " &
objItem.SCSIBus & VbCr & _ "SCSI Logical Unit: " & objItem.SCSILogicalUnit & VbCr & _ "SCSI Port: " & objItem.SCSIPort Next WSCript.Quit
' End of Sample DiskDrive VBScript
From a WMI perspective 1) GetObject("") is the usual VBScript method for connecting to an object. Winmgmts is the
host,
which looks after the CIM schema and objects. In other examples, in place of winmgmts, you may find wscript, LDAP:// or WinNT://. If it would aid your understanding, you could type winmgmt /? at the command prompt. 2)
All WMI scripts begin by
using winmgmts to access the root of the CIM library,
here is the command: Set objWMIService = GetObject("winmgmts:"
& strComputer & "\root\cimv2")
3) Set colProcess = objWMIService.ExecQuery _ is a standard WMI phrase to prepare for the WQL command: Select * from Win32_DiskDrive ". The part we are particularly interested in is
_DiskDrive.
WMI has 4 different types of Win32 Disk object we need to query the LogicalDisk component and not the DiskDrive. From a VBScript perspective 4) strComputer is a classic example of naming a VBScript variable. It is good practice to name variables after the object to
which they refer, in this case the computer. The ".", dot or period means the current machine where the script is running.
Therefore, it would be easy to change the "." for the name of a server on your network. The beauty of using variables is that you only need to set the value once, instead of hunting through the
script for every reference to the computer that you wish to interrogate. 4) What makes scripting so powerful is the
speed with which VBScript
loops through an array of objects or properties, in this instance the loop is controlled by: For Each....In... Next. 5) It is also possible to output the process information not to
the screen but to a text file. In Example 2, we will investigate how WMI calls VBScript to create a file and write the output to file.
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, Solarwinds
have created the
WMI Monitor so that you can examine these gems of
performance information for free. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.
Download your free copy of
WMI Monitor
This script is truly a classic, a routine to apply to other WMI scripts where you need to write information to disk. Let us suppose you want to know if a disk drive is healthy, and the size in MB. Furthermore, you want to know the manufacturer and which SCSI bus each disk is
using.
' DiskWrite.vbs ' Sample VBScript to write Disk Information to a file. ' Author Guy Thomas http://computerperformance.co.uk/ ' Version 1.5 - June
2005 ' ---------------------------------------------------------------'
Option Explicit Dim objFSO, objFolder, objShell, objTextFile, objFile Dim strDirectory, strFile, strText Dim
objWMIService, objItem, colItems, strComputer strDirectory = "e:\logs3\Disks" strFile = "\DiskDrive.txt" strComputer = "."
On Error Resume Next ' Create the File System Object Set objFSO =
CreateObject("Scripting.FileSystemObject")
' Check that the strDirectory folder exists If objFSO.FolderExists(strDirectory) Then Set objFolder = objFSO.GetFolder(strDirectory) Else Set
objFolder = objFSO.CreateFolder(strDirectory) WScript.Echo "Just created " & strDirectory End If
If objFSO.FileExists(strDirectory & strFile) Then Set objFolder = objFSO.GetFolder(strDirectory)
Else Set objFile = objFSO.CreateTextFile(strDirectory & strFile) Wscript.Echo "Just created " & strDirectory & strFile End If
' OpenTextFile Method needs a Const value ' ForAppending = 8
ForReading = 1, ForWriting = 2 Const ForAppending = 8 Set objTextFile = objFSO.OpenTextFile _ (strDirectory & strFile, ForAppending, True)
' WMI Section strText can be adjusted Set
objWMIService = GetObject("winmgmts:\\" _ & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery(_ "Select * from Win32_DiskDrive")
For Each objItem in colItems strText =
"Computer: " & objItem.SystemName objTextFile.WriteLine(strText) strText = "Manufacturer: " & objItem.Manufacturer & _ " " & objItem.Model & " Signature: " & objItem.Signature
objTextFile.WriteLine(strText) Next
objTextFile.Close
If err.number = vbEmpty then Set objShell = CreateObject("WScript.Shell") objShell.run ("Explorer" &" " & strDirectory & "\" )
ElseIf err.number = 424 Then objShell.run ("Explorer" &" " & strDirectory & "\" ) Else WScript.echo "VBScript Error: " & err.number End If
WScript.Quit
' End of VBScript to write to
a file with error-correcting Code
WMI Tutorial Learning Points1) The purpose of this extra script is to output information to a text file. Take the trouble to check the strDirectory and strFile variables. 2) Pay
particular attention the section beginning strText = "Computer: " & objItem.SystemName. I was torn between adding many more properties, and making the script even longer. As the focus of this script
is on the .WriteLine method, is settled for reducing this section. However you could profitably research properties such as : objItem.SCSITargetId from Example 1 above. This page takes you through the second Win32 disk property, Win32_DiskDrive. These properties display physical attributes of the
disk, for example, the Model, number of cylinders, and the SCSI configuration. The icing on the cake is the second example which outputs to a file.
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.
See Also
● Logical Disk ● WMI Home ● Simple WMI Script |