0

I am using the Invoke-Ping function (found here: https://gallery.technet.microsoft.com/scriptcenter/Invoke-Ping-Test-in-b553242a), which is working great. It creates output that looks like this:

> $Info = Invoke-Ping $ComputerNames -ErrorAction SilentlyContinue
> $Info[0..2]

Address      : Machine1
IPV4Address  : 10.10.44.213 
IPV6Address  : 
ResponseTime : 0 
STATUS       : Responding

Address      : Machine2
IPV4Address  : 10.10.4.46 
IPV6Address  :  
ResponseTime : 0 
STATUS       : Responding

Address      : Machine3 
IPV4Address  : 10.10.4.58 
IPV6Address  :  
ResponseTime : 0 
STATUS       : Responding

The problem I'm running into is when I try to do $Info.Address to output machine names. When I type $Info.Address I get

OverloadDefinitions                                                                                        
-------------------                                                                                        
System.Object&, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Address(int )  

I'm sure it's because .Address is already defined but I don't know how to work around this and get to the actual value I need - My object's Address value. I'm sure it's simple but I'm just ignorant... What's the trick to get to my value?

1
  • 1
    $Info | % Address Commented Jan 4, 2017 at 23:45

3 Answers 3

4

use this command this may help you.

$Info | %{$_.Address}

Sign up to request clarification or add additional context in comments.

1 Comment

Short, sweet, to the point, and worked like a charm. Thanks!
1

This is an interesting one. It looks like you've found a bug in the Test-Connection cmdlet. That is what Invoke-Ping uses to ping the computers.

PetSerAl is correct. You can use ForEach to get the correct output. Alternatively you could also manually specify the item in the array that you are looking for. Example:

#Display Address of First item
$info[0].Address


#Display Address of All items
$info | Foreach {$_.Address}
#or
for ($i=0; $i -lt $info.Count; $i++) { $info[$i].Address }

3 Comments

It is not a bug, it is expected behavior. When you "forgot" to put parenthesis after method name, then PowerShell return PSMethod object, describing the method.
If that was true, then why does this work? $ping.status Responding Responding
Because in v3 new feature (Member Enumeration) was introduced. So, when you ask for property or method and collection object does not have that property or method, then that property or method is requested from collection elements.
1

Interesting, haven't run into a case of this, but it appears the root cause is that an array has an Address method.

You can verify like this: Get-Member -InputObject @(), or, with your code, Get-Member -InputObject $Info.

Why is this happening? While it's quite convenient, we're relying on a language feature that will unravel a property from an array, and properties / methods directly on the array will take precedence. This means we need to resort to the various workarounds folks have answered with.

Cheers!

2 Comments

The man himself! Thank you for your blog and the PS scripts you provide! I can't tell you how much time I've saved because of your knowledge and willingness to share!
Thanks for the note, glad any of it helps! And thanks for bringing this up, had never run across this scenario!

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.