Ezine 145 – PowerShell Employs WMI to Interrogate Disks

PowerShell Employs WMI* to Interrogate Disks

One key reason for switching from VBScript to PowerShell is because PowerShell handles WMI* so much more easily than VBScript.

* (Windows Management Instrumentation)

Topics for WMI Interrogates Disks

 ♣

This Week’s Secret

Did you ever stop and think about what your operating system must know?  In a word the operating system knows EVERYTHING.  Normally XP, Vista or Windows 2003 hides much of the detail from displaying in the Windows Explorer shell and its numerous GUIs.  WMI (Windows Management Instrumentation) is a built-in technology, which provides us with a way to retrieve technical detail from the operating system. 

My WMI examples reminds me of climbing a mountain, there is more than one route to the summit.  Moreover, you could climb using a variety of equipment, and you could even call for help from a guide.

This Week’s Mission

This Week’s Mission is to check how much free space is available on each disk.  From the point of view of learning PowerShell, we really are diving in at the deep-end.  I admit that I am obsessed with finding scripts which do useful work, rather than just show off clever scripting techniques.  The price we pay for this gung-ho approach is that some elements may be incomprehensible to the beginner, however that is a risk worth taking.  Also remember that I do provide Learning Points to explain how the script achieves its goals.

As ever, if we break-down the task into individual elements, it is easier to understand what is going on.  My hope is that by dissecting my scripts you can modify commands and improve them to fit your circumstances.

Objective 1: Get one of the WMI objects.  As the acronym WMI (Windows Management Instrumentation) suggests, this method probes the operating system; in our disk example WMI connects specifically to the win32_logicaldisk object.

Objective 2: We want PowerShell to report each disk’s size and free space.  To collect this data we can to issue a SQL query to the LogicalDisk.  In Example 2, observe the command: get-wmiobject -query "Select…..".

Objective 3: Much of the rest of Examples 2 and 3 is concerned with sorting the data, and then formatting the output to produce a table to our liking.

Example 1 (Basic) Command to display disk drives

Instructions:

Pre-requisite: Visit Microsoft’s site and download the correct version of PowerShell for your operating system.

  • Launch PowerShell
  • Copy the lines of code below (into memory)
  • Right-click on the PowerShell symbol
  • Edit –> Paste
  • Press enter to execute the code
  • (Alternatively, just type the commands at the PowerShell prompt)

# Start of Example 1

clear-Host
get-WmiObject win32_logicaldisk

Example 1a: (Format)  Table with automatic column width

# Start of Example 1a

clear-Host
get-WmiObject win32_logicaldisk |format-Table -auto

Learning Points

Note 1: PowerShell commands are not case sensitive.  Get-wmiobject works as well as get-WmiObject.

Note 2: Observe PowerShell and WMI can display properties such as DriveType which do not display in Windows Explorer

Guy Recommends: WMI Monitor and It’s Free!Solarwinds Free WMI Monitor

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.

Download your free copy of WMI Monitor

Trusty Twosome (Get-Help and Get-Member)

Whenever you discover a new PowerShell command such as get-wmiobject, it benefits from being surveyed by what I call the ‘Trusty Twosome’: Get-Help and Get-Member.  To see what I mean, try these two commands:

1) get-help Get-WmiObject
   (help WmiObject -full)  I like the -full because it gives examples of the command.

Get-help shows useful parameters such as: -query, -computername and -credential 

2) get-wmiobject win32_logicaldisk |get-member -memberType property
  (gwmi win32_logicaldisk  | gm -membertype property)

This command reveals a list of properties that you can incorporate in your scripts, for example: DeviceID and DriveType.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Example 2 (Grouping) Disk drives by type.

# Start of Example 2

clear-Host
get-wmiobject -query "Select * from win32_logicaldisk" |sort mediatype |`
format-table DeviceId, DriveType, Size, FreeSpace -auto -groupby MediaType

Learning Points

Note 1:  This example uses the powerful -query to select (* all) properties from the win32_logicaldisk object.

Note 2:  Once selected, we can sort the data and even group the disks in the output.

Note 3:  If you look carefully you can see the backtick (`) at the end of line two. The purpose of this tiny symbol is to tell PowerShell that the same command continues on the next line.

Example 3 (Better) Displaying FreeSpace as a percentage

# Start of Example 3

clear-Host
$Jumbo = 1024 * 1024 * 1024
"Disk: Free Space"
$DiskObj = get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 }
$DiskObj | foreach { $_.DeviceID, (($_.freespace /$Jumbo) / ($_.size / $Jumbo))}

Learning Points

Note 1:  This example does not use the -query parameter.  Instead it employs the where clause to filter the data and pass the stream to the foreach loop, which display each disk drive. 

Note 2:  $Jumbo is a variable to assist with converting the bytes to gigabytes.

Note 3:  My purpose is to show you how flexible PowerShell is at achieving slightly different objectives. (Remember my analogy with mountain climbing, many ways of reaching the summit.)

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 3a (Best) Displaying FreeSpace as a percentage

# Start of Example 3a

clear-Host
$Jumbo = 1024 * 1024 * 1024
"Disk: Freespace %"
$DiskObj = get-WmiObject Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 }
$DiskObj | foreach { "{0,5} {1,5} {2,10:p}" -f `
$_.DeviceID, [int]($_.freespace / $Jumbo), (($_.freespace /$Jumbo) / ($_.size / $Jumbo))}

Learning Points

Note 1:  Observe the -f formatting technique.  What -f does is give greater control over the column widths.  Each of the three pairs of {braces} represents a data item in the last line.  Another benefit of the -f formatting is to enable us to display the results as a percentage {2,10:p}.

Note 2:  $Jumbo is a variable to assist with converting bytes to gigabytes.  I expect you spotted the usual maths operators to divide (/) and display as whole numbers [int].

Tip:  Keep an eye on the type of bracket.  Whether PowerShell uses (parenthesis) {braces} or [square] brackets is highly significant. As a rule of thumb these types of bracket mean: (compulsory), {script block}, [optional].

Challenges

Challenge 1: Experiment with the display of Examples 1a and 2, remove -auto.  You could also try format-list instead of format-table.

Challenge 2: In Example 3a, edit the properties, for example, change [int]($_.freespace / $Jumbo), to ($_.freespace / $Jumbo)

Challenge 3: In Example 2 attempt to alter the ‘Sort’ and ‘-groupby’ criteria.

Summary of Displaying Disk Information

Our mission was to employ get-wmiobject to display disk information.  By using 3 or 4 examples, I hope that you gain perspective of the command.  My greatest joy would be if you would amend one of my scripts to suit your circumstances.

If you like this page then please share it with your friends

 


See more PowerShell share examples including WMI

PowerShell WMI   • Create Win32_Share   • WMI Shares   • Free Permissions Analyzer Tool

Get-Acl  • PowerShell Share Error Codes   • Win32_ComputerSystem  • PowerShell 3.0 CIM

Windows PowerShell  • Free WMI Monitor  • Cacls   • Query   • PowerShell Printer Scripts

Please email me if you have a example scripts.  Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.