Friday Fun: A GridView Drive Report

I’ve been experimenting with different techniques to work with PowerShell in graphical ways, but without resorting to complex solutions such as WinForms or ShowUI. For today’s Friday Fun I have a little script that presents a drive usage report using WMI and Out-GridView. As always, my goal with these articles is to impart a nugget of useful information, regardless of whether you need the complete solution.

Getting the drive data with WMI is pretty straightforward.

But what I want is something I can send to Out-Gridview that will give me a graphical representation of drive utilization. So I’ll take this basic command and pipe it to Select-Object, and add a few custom properties.

My custom properties reformat values into MB and a Free percentage. These values are formatted as numbers so they can be sorted. That much would be fine if you wanted to write that to the pipeline. But I’m going to get graphical so I also define a property called FreeGraph. The value is simply the “|” character displayed once for each percent of free space. Sure, I could write this to the pipeline and see something like this:

But where this gets really interesting is where I pipe $data to Out-GridView. Here’s an example where I queried several computers.

Now I have a graphical report that I can filter and sort. You may have to manually resize the display and adjust columns but you get the idea. But as they say on late night TV, wait there’s more.

If you have PowerShell v3, Out-Gridview now supports passthru. You can select one or more objects in Out-GridView and they will be written to the pipeline. This leads to some interesting opportunities. Here’s a variation that opens the selected drives in a new gridview window, displaying all the WMI properties.

This gridview allows me to select multiple entries.

When I click OK, the objects are piped back to PowerShell and in this case back to Out-Gridview.

Or, maybe you simply need to open the drive on the remote machine. Since I’m querying logical disks, they should each have an administrative share which I can construct from the drive object, and then open using Invoke-Item.

In these examples Out-Gridview is set to allow multiple selections. If you prefer to limit selection to one object, then use -Outputmode Single in place of -Passthru.

I’ve put all of these commands in a script you can download and try for yourself.

Variable Validation

In PowerShell v3 there is a new feature you might not be aware of that could save you pain and headaches. This is something you could use in scripting as well as the console. In fact, I think using it in the console is an especially smart idea.

In PowerShell v2 scripts and functions we had the ability to add validation tags to parameters. These tags could perform different validation tests on a parameter value. If the value failed, the script or function would throw an exception. I’d rather have the command fail to start than to fail halfway through. Now in PowerShell v3 we can use these same tags on variables in our scripts and even the console. I’ve written about a number of these validation tags in the past on my blog. Here’s an example of what we can do in v3.


PS C:\> [validateRange(1,10)]$i=5

In this example I’ve added a validation test to verify that any value for $i is between 1 and 10. I can use the variable as I normally would. Even change the value.


PS C:\> $i*2
10
PS C:\> $i+=1
PS C:\> $i*2
12

But watch what happens when I try to use a value outside of the accepted range:


PS C:\> $i=30
The variable cannot be validated because the value 30 is not a valid value for the i variable.
At line:1 char:1
+ $i=30
+ ~~~~~
+ CategoryInfo : MetadataError: (:) [], ValidationMetadataException
+ FullyQualifiedErrorId : ValidateSetFailure

I get an exception. This is much better than setting a value that will cause another error later. The sooner you can detect potential problems the better.

Perhaps this example isn’t compelling. But there are other validation tests you might want to take advantage of. Maybe a regular expression pattern.


PS C:\> [validatepattern({\\\\\w+\\\w+})]$unc="\\server01\share"

If I later decide to change the value, the validation will help me catch typos.


PS C:\> $unc="\server02\data"
The variable cannot be validated because the value \server02\data is not a valid value for the unc variable.
At line:1 char:1
+ $unc="\server02\data"
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [], ValidationMetadataException
+ FullyQualifiedErrorId : ValidateSetFailure

The bottom line is that if your variable values will change, using a validation tag will ensure new values will work. If you don’t want to sift through the blog learning more about the validation tags, take a look at my Scripting Help module.

PowerShell 3.0 Easy Rider

Today I gave an Live Meeting presentation for the PowerShell Virtual User Group. When the recording is posted I’ll update this post. I spoke about new features in Windows PowerShell 3.0 that I think will make it easier for people to use PowerShell more efficiently. I think they even might find it fun. Here is what I covered.

 

 

AGENDA
 New Help Options
 Default Parameter Values
 Simplified Syntax
 Improved Tab Completion
 Redirection Options
 Improved ISE Experience

The presentation started with some introductory slides, but most of the presentation was demo. As promised here is my presentation and a zip files with my demos and sample script.

Here is the link to the recording