2

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

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

4 Answers 4

5

This is most likely the result of of the IPAddresses property of your object containing an array. The output you're seeing is stylized for display purposes, so it's not a string you would have to parse. Assuming your object is $obj, you should be able to do either of these:

$obj.IPAddresses[0]
$obj.IPAddresses | Select-Object -First 1
Sign up to request clarification or add additional context in comments.

1 Comment

I think I tried the first of your ideas, but the 2nd looks like it might work! Smart.
1

One solution is to use split function to convert the string into array and work with that like in the next steps:

  1. Split the string into an array using the split function (comma is the item delimiter).
  2. Grab the first item of the array (or whatever needed) and then also sanitize it (remove unnecessary curly bracket).

Example below:

$str = "{10.60.50.40, fe80::5ddf:a8f4:e29c:b66}"

$strArr = $str.Split(",")

Write-Host $strArr[0].Replace("{", "")

1 Comment

Was trying to avoid that, as we don't always know that the first one will be the IPv4 Address!
0

This is what I ended up doing:

$str = ... | Select-Object IPAddresses | ForEach {$_.IpAddresses}
Write-Host $str[0]

1 Comment

The ForEach part can be eliminated if you change the middle to Select-Object -ExpandProperty IPAddresses.
0

Depending on the source of your IPAddresses, this might not be optimal. You might get multiple IPAddresses per devices.

You might want to combine both approaches:

$str = ... | Select-Object -ExpandProperty IPAddresses | Select-Object -First 1

This will return the First IP address in your list per device.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.