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.)

2

4 Answers 4

1

This is what I have so far:

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

Returns the first one.

0

Try it like this:

$myResultSet | foreach { $_.IPAddresses[0] }
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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .