Reputation: 57
I'm trying to measure latency using Test-NetConnection. I've gotten this to work well with Test-Connection, but it's not available everywhere.
Here is what I've done with Test-Connection:
PS:>Test-Connection 8.8.8.8 -count 1 | select ResponseTime
ResponseTime
------------
28
Test-NetConnection does return a property containing latency.
PS:>Test-NetConnection 8.8.8.8
ComputerName : 8.8.8.8
RemoteAddress : 8.8.8.8
InterfaceAlias : eth0
SourceAddress : REMOVED
PingSucceeded : True
PingReplyDetails (RTT) : 28 ms
But when I try to refer to this property, I don't get the value.
PS:>Test-NetConnection 8.8.8.8 | select PingReplyDetails
PingReplyDetails
----------------
System.Net.NetworkInformation.PingReply
How can I get the actual value out of the command?
Upvotes: 2
Views: 1835
Reputation: 2066
Try this... I've broken it down into two steps for a bit more clarity, but you can combine into a single statement on your own if you'd like.
$data = Test-NetConnection 8.8.8.8 | select -ExpandProperty PingReplyDetails
$data.RoundtripTime
Appears to work for me, as you are going for. Since your question includes different attempts, I trust you should be able to customize to fit your exact needs.
In short, use -ExpandProperty
to bring out the details in an object.
Upvotes: 1
Reputation: 35408
Well the problem is that PingReplyDetails (RTT)
is not a real property as you can see with the following command and its output, where this 'property' is missing.
PS > Test-NetConnection SomeHost | Get-Member
TypeName: TestNetConnectionResult
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
AllNameResolutionResults Property System.Object AllNameResolutionResults {get;set;}
BasicNameResolution Property System.Object BasicNameResolution {get;set;}
ComputerName Property string ComputerName {get;set;}
Detailed Property bool Detailed {get;set;}
DNSOnlyRecords Property System.Object DNSOnlyRecords {get;set;}
InterfaceAlias Property string InterfaceAlias {get;set;}
InterfaceDescription Property string InterfaceDescription {get;set;}
InterfaceIndex Property uint32 InterfaceIndex {get;set;}
IsAdmin Property bool IsAdmin {get;set;}
LLMNRNetbiosRecords Property System.Object LLMNRNetbiosRecords {get;set;}
MatchingIPsecRules Property ciminstance[] MatchingIPsecRules {get;set;}
NameResolutionSucceeded Property bool NameResolutionSucceeded {get;set;}
NetAdapter Property ciminstance NetAdapter {get;set;}
NetRoute Property ciminstance NetRoute {get;set;}
NetworkIsolationContext Property string NetworkIsolationContext {get;set;}
PingReplyDetails Property System.Net.NetworkInformation.PingReply PingReplyDetai...
PingSucceeded Property bool PingSucceeded {get;set;}
RemoteAddress Property ipaddress RemoteAddress {get;set;}
RemotePort Property uint32 RemotePort {get;set;}
SourceAddress Property ciminstance SourceAddress {get;set;}
TcpClientSocket Property System.Net.Sockets.Socket TcpClientSocket {get;set;}
TcpTestSucceeded Property bool TcpTestSucceeded {get;set;}
TraceRoute Property string[] TraceRoute {get;set;}
As it turns out, it is just a kind of formatting sugar which is defined for the result type (TestNetConnectionResult as seen above) of this Cmdlet. The formatting description for this can be retrieved via the following command:
Get-FormatData TestNetConnectionResult | `
Select -ExpandProperty FormatViewDefinition | ? Name -eq DefaultView | `
Select -ExpandProperty Control | `
Select -ExpandProperty Entries | `
Select -ExpandProperty Items | ? Label -eq "PingReplyDetails (RTT)" | `
Select -ExpandProperty DisplayEntry | `
Select -ExpandProperty Value
which returns
$_.PingReplyDetails.RoundTripTime.ToString() + " ms";
With that information in place you can do the same as well e.g. with the following:
Test-NetConnection 8.8.8.8 | Select @{N = "PingReplyDetails (RTT)"; E = {$_.PingReplyDetails.RoundTripTime.ToString() + " ms"}}
Upvotes: 3