Powershell parse object / string

1

Learning powershell, trying to find out how to parse the first value from this resultset:

IPAddresses
-----------
{10.60.50.40, fe80::5ddf:a8f4:e29c:b66}

Normally I would just look it up, however, I don't know if {x, x} is a standard datatype of sorts in Powershell land.

Do I have to do rough string parsing, or is there some standard command to extract the first one, such as:

... | Select-Object IPAddresses | Select-String [0]

(I just made the select string part up. I'm lost.)

dthree

Posted 2015-05-03T22:10:54.063

Reputation: 213

you could try creating a tupple: http://blogs.technet.com/b/heyscriptingguy/archive/2014/09/02/using-a-tuple-in-powershell.aspx Then you can access them with $var.Item1 or $var.Item2

– Frank Thomas – 2015-05-03T22:24:14.480

Tried that, doesn't seem to parse it. It seems IPAddresses the piece of a tuple already, so you can't break it down further than that. – dthree – 2015-05-03T22:31:56.620

Answers

1

This is what I have so far:

... Select-Object IPAddresses | ForEach {$_.IPAddresses}[0]

Returns the first one.

dthree

Posted 2015-05-03T22:10:54.063

Reputation: 213

0

Try it like this:

$myResultSet | foreach { $_.IPAddresses[0] }

dangph

Posted 2015-05-03T22:10:54.063

Reputation: 3 478

0

Try

$myResultSet | Select-Object -First 1 IPAddress

Your result set is a collection of some type. The Select-Object cmdlet is taking the first item in that collection and then filtering to only the IPAddress property which is then displayed.

On my machine the output of the above command looks like this. Yours will likely differ in that the address returned will be different

IPAddress                                                                                                              
---------                                                                                                              
fe80::5581:4fbc:fc22:ec79%13                                                                                           

You get the table because by default PowerShell puts the output through Format-Table to make a nice display.

To access just the IP address itself you can modify the expression slightly:

$myResultSet.IPAddress|Select-Object -First 1

or, by using dot notation and an index into the collection you can avoid using Select-Object entirely like so:

$myResultSet[0].IPAddress

The above refers directly to the IPAddress property of the first item in the $myResultSet collection, which has an index of zero hence $myResultSet[0]

Either of these will return just the IP address itself like so:

fe80::5581:4fbc:fc22:ec79%13

Crippledsmurf

Posted 2015-05-03T22:10:54.063

Reputation: 1 442

0

First, you don't have to guess what type IPAddresses is, you can get it easily like this:

$myResultSet[0].IPAddresses.GetType()

Probably it will be some sort of collection (array), so you can use Select-Object with ExpandProperty parameter:

$myResultSet | Select-Object -ExpandProperty 'IPAddresses' | Select-Object -First 1

Or you can use property dereference operator and\or index as Crippledsmurf suggested

beatcracker

Posted 2015-05-03T22:10:54.063

Reputation: 2 334