WMI - Logical Disk Properties
Introduction to WMI Disks - Logical Disk Properties
When you investigate Disk activity, whether with WMI scripts or Performance Monitor, you should research more than one disk object. The classical divide is between 'Logical' or 'Physical' disk. In the case of WMI we have the following two Win32 objects:
Win32_LogicalDisk - Partition size, Drive Type and FreeSpace. (This page)
Win32_DiskDrive - Physical Disk data. Based on the number of blocks, tracks or sectors. See Here
Once you have mastered the basics of WMI, and Win32_LogicalDrive, you may wish to investigate extra properties such as DriveType or MediaType. What happens with scripting is that when you discover new properties, your mind races ahead and thinks of ways to exploit the new knowledge. For example, the property DiskType could give you the idea of taking action depending on whether the partition is, Fixed Hard Disk, CD, or 'Removal Drive'. To give a specific example, if the drive is Removable then you probably don't want to install software or save a profile on that partition.
Topics for WMI - Disks
Let us suppose you need to know how many partitions there on a Windows 2003 Server, moreover, you wish to record the size and free space of each partition. In fact I have selected about a dozen LogicalDisk properties and rejected a further 20. My criteria were, does the property usually return a result and is this a mainstream feature of a disk.
Prerequisites for your Logical Disk Information WMI Script
None. Just take care with the _ Underscores which indicate the instruction wraps to the next line.
Instructions for your Logical Disk Information WMI Script
Script to Interrogate Logical Disk
From a WMI perspective
1) GetObject("") is a well-known VBScript method for fetching an instance of an object. Winmgmts is the shell, 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.
You will soon appreciate that the WMI section of the script begins by
calling winmgmts. Specifically, winmgmts gives access the root of the CIM library,
here is the command:
3) Set colProcess = objWMIService.ExecQuery _ is a standard WMI phrase, which prepares for the WQL command: Select * from Win32_LogicalDisk
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. On later pages, we will investigate how WMI calls VBScript to create a file and write the output to file. However, for the present we will stick with Windows Script Host message boxes.
Note: If it's physical disk information that you're interested in then there is a different Win32 object called DiskDrive, which reports on properties such as SCSI Bus, track, sector or absolute disk size. See here how to interrogate the Physical Disk.
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory or Exchange Server. Give this WMI monitor a try - it's free.
Many WMI tasks require specific information, for example before you install software you need to know the amount of free space and possibly the drive type. This script interrogates the operating system to see whether the drive type is removable, local hard disk or some other type.
WMI Tutorial Learning Points
From a WMI perspective
1) This script focuses on the Win32_LogicalDisk, DriveType property. However, it also reports on the size of the partitions.
From a VBScript perspective
2) I love the Select Case statement. Wherever there are multiple outcomes I see if Select Case would be less cumbersome than multiple If then.. Else statements.
DriveType can return any number from 1 to 6. Moreover, as a number, the output is less useful than a descriptive string value. In particular, we would wish to know if the partition is "Removable Drive", "Compact disk" or "Local hard disk". Dealing with a number means you have to memorise whether DriveType 2 = Removable Drive or Local had drive. Wearing your VBScript hat, displaying, Drive Type : Local hard drive, costs little and gives a more meaningful output than, Drive Type : 3.
3) When it comes to outputting the disk size, removable disks tend to be in the mega range whereas fixed disks are more likely to be in gigabytes.
In case you thought I did not like the If construction, here I happily employ, If .. Then .. Else... End If statement. Note how the size of the removable disk is divided by 1048576 (1024 x 1024), and that the fixed disk is divided by a number 1024 times bigger (1024 x 1024, 1024). For me it's a matter of horses for courses, sometimes Select Case wins, sometimes If...Then, Else .. End If is more effective.
SolarWinds' Config Generator is a free tool, which puts you in charge of controlling changes to network routers and other SNMP devices. Boost your network performance by activating network device features you've already paid for.
Guy says that for newbies the biggest benefit of this free tool is that it will provide the impetus for you to learn more about configuring the SNMP service with its 'Traps' and 'Communities'. Try Config Generator now - it's free!
Just a cosmetic change on the previous example to display all the disk drives in one message box.
Now that you are growing in experience, this maybe a suitable time just to 'play' with the script. You could start with cosmetic changes to the separators. Replacing === with ####. Then progress to changing " Mega Bytes" to " Mb". My point is that making even simple changes you will miraculously gain in confidence and soon you will be making more widespread changes.
The Win32_LogicalDrive object exposes properties such as the type of partition and how much free space is on the disk. As with most of the WMI scripts, it effortlessly cycles through the properties and displays the values for each partition or disk drive in a message box.
If you like this page then please share it with your friends
See more VBScript WMI examples: