Ezine 201 - WMI Classes to Examine DisksEzine 201 - WMI Classes to Examine DisksThis week we are going to combine PowerShell and WMI and examine disk properties. Once we master the basics of get-Help and get-Member, then we are free to explore all the WMI disk objects. Topics for WMI Classes to Examine Disks
♣ This Week's SecretThree good things happen whenever I rummage through WMI's classes. Invariably, I discover something about my machine; in the case of Win32_LogicalDisk I found a forgotten partition. Working with WMI objects always teaches me something new about pure PowerShell commands, for example, Math truncate. Another source of pleasure is that research one WMI leads to interesting sister classes such as Win32_DiskQuota or Win32_DiskDrive. This Week's Mission - Research WMI Disk ClassesMy mission this week is to introduce you to a subset of WMI's classes which features disk properties. While I focus on just one class, Win32_LogicalDisk, please just regard this as an example. It is my hope that you will discover more disk objects, and thus solve a particular problem on your computer. Get-Help Get-WmiObject -fullAll top athletes practice basic routines until they become second nature. When top PowerShell scripters start using a cmdlet, such as get-WmiObject, they invariably run get-Help and get-Member. get-Help get-WmiObject -full Let us research WmiObject classes containing disk. get-WmiObject -list | Where {$_.name -match 'disk' -and ` Note 1: Please note the backtick which enables the command to word-wrap onto the second line, in particular, there should be no space after the backtick (`) Now to list the Properties of LogicalDisk Get-WMIObject Win32_LogicalDisk |get-Member ` Note 2: The purpose of the -memberType line is to filter just for properties, and to excluding those beginning with __. Basic Disk Reporting Get-WmiObject Win32_LogicalDisk | format-Table Name, Size Fancy Disk Reporting - Same Output, But with Better Formatting. Get-WmiObject Win32_LogicalDisk | ft Name, @{Label = "Disk
GB"; ` Note 3: Ft is an alias for format-Table. Incidentally, gm is an alias for get-Member. How to Measure Freespace Let us analyze how we appened the additional command, which adds a free diskspace column to the results. @{Label = "Free GB";Expression={[math]::truncate($_.freespace / 1GB)}} @{Label tells PowerShell we want a nice heading, otherwise we get a
confusing
literal: The business end of this command is split into two parts, [math]::truncate, and divide by 1 gigabyte. Sandwiched in between is $_.freespace, where freespace is a property of Win32_LogicalDisk. Get-WmiObject Win32_LogicalDisk |ft Name, @{Label = "Disk GB"; ` WMI Monitor Details
| |||||
Custom Search
|
Guy Recommends: WMI Monitor and It's Free!
|
|
Home Copyright © 1999-2012 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |