New-Object from a hash table in a script block

Creating objects using a hash table in a scripblock.

A couple of the useful properties of scriptblocks are that they delay execution, and they expose code as text. This can be used to both simply your code and make it easier to read and maintain.
Here’s a code snippet for creating custom ps objects from parsed text, that leverages these properties:

$input_data =
$regex =
$props = {
@{
}
}
$proplist = $props.tostring().split("`n") -match "^\s*(\S+)\s*=.+$" -replace "^\s*(\S+)\s*=.+$",'$1'

$input_data | foreach-object {
if ($_ -match $regex){
new-object psobject -property (&$props) | select $proplist
}
}

Here’s how it might look when used to create ps objects from the output of netstat:

$input = netstat -n
$regex = "\s\s(\S+)\s+(\S+):(\S+)\s+(\S+):(\S+)\s+(\S+)"

$props = {
@{
   State = $matches[6]
   Protocol = $matches[1]
   LocalAddress = $matches[2] -as [ipaddress]
   LocalPort = $matches[3]
   ForeignAddress = $matches[4] -as [ipaddress]
   ForeignPort = $matches[5]
   }
}
$proplist = $props.tostring().split("`n") -match "^\s*(\S+)\s*=.+$" -replace "^\s*(\S+)\s*=.+$",'$1'

$input_data | foreach-object {
if ($_ -match $regex){
new-object psobject -property (&$props) | select $proplist
}
}

Add an input source ($input_data) , and a match expression that will select and group the values you want to capture ($regex), and then within the hash table brackets a list of the property names you want to use, the group that holds the value you want it to have, and whatever typecasting you want to do.

Having the hash table in a scriptblock lets you move that table definition out of the foreach loop, and relocate it anywhere in the script prior to where it’s being invoked (delayed execution).

Now, creating objects from hash tables is very efficient, but has the downside of producing objects who’s properties aren’t in any predicable order. The easiest way to remedy this is to use select-object to put the properties in desired order. With the hash table definition now in a scriptblock, it’s code is exposed as text, and the list of property names can be parsed out of that text. The $proplist expression will parse property names into an array, and that’s used for the select-object argumentlist. However they’re ordered in the hash table is how they will be ordered in the object.

Advertisements

2 responses to “New-Object from a hash table in a script block

  1. Pingback: Creating objects from ordered hash tables in Powershell V3 | The Powershell Workbench

  2. Pingback: PowerShell eigene Objekte erstellen Custom Objects - PowerShell Get-Script -Name Peter Kriegel | Management

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s