Use PowerShell
The Shell Is Calling
The Shell Is Calling
I really enjoy Jeffery Hicks’s Prof. PowerShell column. In two of his recent columns (Perfect Properties and Label Me Perfect), Jeffery goes in to “calculated properties”.
A calculated property is a hashtable passed to the Select-Object or Format-* cmdlets as one of the properties to return.
Here is an example from Label Me Perfect:
PS C:\> get-wmiobject Win32_logicaldisk -filter “drivetype=3″ `
-computer “mycompany-dc01″ | Format-table DeviceID,VolumeName,`
@{label=”Size(MB)”;Expression={$_.Size/1mb -as [int]}},`
@{label=”Free(MB)”;Expression={$_.FreeSpace/1MB -as [int]}},`
@{label=”PercentFree”;Expression={
“{0:P2}” -f (($_.FreeSpace/1MB)/($_.Size/1MB))}} -autosize
That is a whopper to type at the command line or to try to read in a script.
In order to increase readability and resuse, you can assign those hash tables to a variable and use that variable as one of the parameters to Select-Object or one of the Format-* cmdlets.
PS C:\> $Size = @{label=”Size(MB)”;Expression={$_.Size/1mb -as [int]}}
PS C:\> $FreeMB = @{label=”Free(MB)”;Expression={$_.FreeSpace/1MB -as [int]}}
PS C:\> $PercentFree = @{label=”PercentFree”;Expression={“{0:P2}” -f (($_.FreeSpace/1MB)/($_.Size/1MB))}}PS C:\> get-wmiobject Win32_logicaldisk -filter “drivetype=3″ `
-computer “mycompany-dc01″ | Format-table DeviceID,VolumeName, `
$Size, $FreeMB, $PercentFree -autosize
This makes it a bit easier to read, and also makes those hash tables available for further use in additional statements without needing to retype them.
PSMDTAG: FAQ: Calculated Properties
April 22, 2009 - 6:06 am
Very nice… I love it! This just show that you can go many different directions when you’re building your script.
April 22, 2009 - 6:08 am
Thanks Max!
April 22, 2009 - 6:22 am
Great suggestions. I was trying to stick to a one-liner but scriptblocks make total sense and perfectly appropriate for a scripted solution. Thanks for your interest and support.
April 22, 2009 - 6:36 am
Like I said Jeff, I enjoy your columns. Even though I’ve been using PowerShell for quite some time now, your column provides a great reminder as to some less obvious or common features and capabilities.
April 22, 2009 - 7:38 am
Nice use of HashTables. I find myself using them everywhere, especially for simple mapping/lookups. Good stuff!
April 22, 2009 - 10:43 am
Thanks Wes. Hash tables are a great lightweight for mappings and lookups. I’ve just used them that way for a data conversion project I’m working on.
April 23, 2009 - 6:30 am
One can also use variables in the Expression script blocks, but they must be defined beforehand and sometimes as :
$expression_variable = but I am not sure when to execute the script block. The usage is then Expression = $expression_variable
April 25, 2009 - 9:00 am
Good tip! Especially if you have a pretty complicated scriptblock.
April 25, 2009 - 8:51 am
This is great tip and shows how adding a few extra lines of code can really make things cleaner and easier to read.
April 25, 2009 - 9:06 am
One of the great strengths of PowerShell is that the commands can read like a sentence. A person with a basic understanding of PowerShell should be able to look at a script and have an understanding of what is happening.
Additionally, Shared scripts (whether shared internally in your organization or externally in to the world) will be read far many more times then they are written. In order to increase the readability of a script, using variable and function names that are very descriptive make it that much easier to maintain and update for anyone.