我正在尝试使用 Test-NetConnection 测量延迟。我已经让它与测试连接很好地配合使用,但它并非在所有地方都可用。
这是我对测试连接所做的操作:
PS:>Test-Connection 8.8.8.8 -count 1 | select ResponseTime
ResponseTime
------------
28
Test-NetConnection 确实返回包含延迟的属性。
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
但是当我尝试引用此属性时,我没有得到该值。
PS:>Test-NetConnection 8.8.8.8 | select PingReplyDetails
PingReplyDetails
----------------
System.Net.NetworkInformation.PingReply
如何从命令中获取实际值?
最佳答案
问题是,PingReplyDetails (RTT)
不是真正的属性,正如您通过以下命令及其输出看到的那样,其中缺少此“属性”。
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;}
事实证明,它只是一种为此 Cmdlet 的结果类型(如上所示的 TestNetConnectionResult)定义的格式化糖。可以通过以下命令检索格式描述:
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
返回
$_.PingReplyDetails.RoundTripTime.ToString() + " ms";
有了这些信息,您也可以执行相同的操作,例如具有以下内容:
Test-NetConnection 8.8.8.8 | Select @{N = "PingReplyDetails (RTT)"; E = {$_.PingReplyDetails.RoundTripTime.ToString() + " ms"}}
关于.net - 在 Powershell 中使用 .NET 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38019258/