PowerShell

PowerShell: Building Objects with Custom Properties with Select-Object

What if you are not satisfied with the default output of PowerShell? Well, there are many ways to come closer to the desired result. In this blog article I am going to create Custom Objects with PowerShell.

The Task

The task is to test a connection by using Test-NetConnection. The output must be formatted as follows:

  • Computer Name
  • Destination Address
  • Ping Success / Time Out

It should look like this:

Unbenannt.JPG

Where do we start? Well, first we have to learn more about Test-NetConnection. Let’s run the command without any other parameters:


Test-NetConnection sid-500.com

2.JPG

Ok, we need ComputerName, RemoteAddress and Failure/Success. It’s all there! So, we can easily modify the ouput with Select-Object.


Test-NetConnection sid-500.com | Select-Object ComputerName,RemoteAddress,PingSucceeded

Unbenannt.JPG

End of Subject, right? No. For any other reason we have to find the property which returns “Success” and “Time Out” instead of only “True” and “False”. I am talking about this:

Unbenannt.JPG

Ok, let’s find that property. Get-Member makes it happen. Here it is:


Test-NetConnection sid-500.com | Get-Member -MemberType Property

Unbenannt.JPG

Now we can try to include PingReplyDetails in our One-Liner:


Test-NetConnection sid-500.com | Select-Object ComputerName,RemoteAddress,PingReplyDetails

Unbenannt.JPG

Something went wrong here. The property PingReplyDetails refers to another class. So, we have to use -Expandproperty to look behind it.


Test-NetConnection sid-500.com | Select-Object -ExpandProperty PingReplyDetails

Unbenannt.JPG

Aaah, there it is! We’ve found what we were looking for! But we do not need the whole object. We only need the value “Success”.  How to get it? The answer is: Use Select-Object -Expandproperty again!


Test-NetConnection sid-500.com | Select-Object -ExpandProperty PingReplyDetails | Select-Object -ExpandProperty Status

Unbenannt.JPG

Nice one! But how to put this into our command?

unbenannt5.jpg

Creating a Custom Property

The answer is: We have to build our own custom property. Let’s show a preview what we want to accomplish:


Test-NetConnection sid-500.com | Select Computername,RemoteAddress, @{n='Success/Time Out';e={$_ | Select-Object -ExpandProperty Pingreplydetails | Select-Object -ExpandProperty Status}}

The first part should be clear. It’s the same as above.


Test-NetConnection sid-500.com | Select Computername,RemoteAddress

The second part is more interesting. In this part we build our custom object:


@{n='Success/Time Out';e={$_ | Select-Object -ExpandProperty Pingreplydetails | Select-Object -ExpandProperty Status}

@ defines an array. This array has a name and the name is defined by “n=‘Success / Time Out'”. Then we have to deal with the Pingreplydetails and Status. For this we use $_. $_ is a variable which iterates over each element passed from the pipe. And then we expand Pingreplydetails and Status with an expression statement (e=). This kind of array is also called hashtable.

The final command

And finally here it is.


Test-NetConnection sid-500.com | Select Computername,RemoteAddress, @{n='Success/Time Out';e={$_ | Select-Object -ExpandProperty Pingreplydetails | Select-Object -ExpandProperty Status}}

Unbenannt.JPG

Categories: PowerShell

Tagged as: ,

3 replies »

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.