Thursday, March 3, 2016

Finally, objects is making sence

I've been struggling with one last part of MS PowerShell for a while now, namely the internal data structures of PowerShell objects (and all .Net objects for that matter).

They are neat, but how do I define custom outputs in an easy way to read in my script code?

You've all read about Add-Member -type and New-Object System.Object.
They do work, but they are a pig to write.

Now, you might say "But why don't simply use hash tables?".
Well, they can't be ordered in PowerShell v2. You'll need PowerShell v3 to build it, i.e.

[ordered]@{First = "Sven"; Last = "Karlsson"}

and then there's the matter of hash tables only containing two values (key and value) which makes the construct confusing :/

Say I want to output a phone list as an object in PowerShell.

$propertyhashSven = @{}
$propertyhashSven.first = "Sven"
$propertyhashSven.last = "Karlsson"
$propertyhashSven.phone = "040-11 12 13"

> $propertyhashSven

Name                           Value
----                           -----
last                           Karlsson
phone                          040-11 12 13
first                          Sven


> $sven = New-Object PSObject -Property $propertyhashSven

> $sven

last                      phone                       first
----                      -----                       -----
Karlsson                  040-11 12 13                Sven


The properties is not ordered at ALL :/

But how about this...

First thing is to define the object. The easiest way I found is to first create my own data type in C#.

add-type @"
 public struct contact {
    public string First;
    public string Last;
    public string Phone;
 }
"@


Then, simply create the objects using that data type.

> $sven = New-Object contact
> $kalle = New-Object contact


And then populate it with some properties

> $sven.First = "Sven"
> $sven.Last = "Karlsson"
> $sven.Phone = "040-11 12 13"

> $kalle.first = "Kalle"
> $kalle.last = "Svensson"
> $kalle.phone = "08-555 555"

And then finally wrapping it up in a phone book as an array.

> $phonebook = @($sven,$kalle)

And there you go :)

> $phonebook

First                     Last                        Phone
-----                     ----                        -----
Sven                      Karlsson                    040-11 12 13
Kalle                     Svensson                    08-555 555


Want to sort it by first name? No problems.

> $phonebook | Sort-Object First

First                     Last                       Phone
-----                     ----                       -----
Kalle                     Svensson                   08-555 555
Sven                      Karlsson                   040-11 12 13


And adding more entries is just a matter of expanding the array...

> $sture = New-Object contact
> $sture.first = "Sture"

> $phonebook = $phonebook + $sture 

> $phonebook

First                     Last                       Phone
-----                     ----                       -----
Sven                      Karlsson                   040-11 12 13
Kalle                     Svensson                   08-555 555
Sture


And hopefully, it won't break anything ;)

1 comment:

  1. Well. It will only handle C# data types.

    So say goodbye to the flexible datatype handling of PowerShell such as [ipaddress] or dynamic typing where the type of a property isn't set until definition.

    ReplyDelete