在Power Shell中使用.NET属性

在Power Shell中使用.NET属性,.net,powershell,.net,Powershell,我正在尝试使用TestNetConnection测量延迟。我已经把它和测试连接很好地结合起来了,但是它并不适用于任何地方 以下是我对测试连接所做的工作: PS:>Test-Connection 8.8.8.8 -count 1 | select ResponseTime ResponseTime ------------ 28 Test NetConnection确实返回包含延迟的属性 PS:>Test-NetConnection 8.8.8.8 ComputerName

我正在尝试使用TestNetConnection测量延迟。我已经把它和测试连接很好地结合起来了,但是它并不适用于任何地方

以下是我对测试连接所做的工作:

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"}}

试试这个。。。为了更清楚一点,我把它分为两个步骤,但是如果你愿意,你可以自己组合成一个单独的语句

$data = Test-NetConnection 8.8.8.8 | select -ExpandProperty PingReplyDetails
$data.RoundtripTime
似乎对我有用,就像你要做的那样。由于您的问题包括不同的尝试,我相信您应该能够定制以满足您的确切需求


简而言之,使用
-ExpandProperty
显示对象中的详细信息。

嗨,我想知道
测试连接在哪里不可用?@sodawillow-检查您的
$PSVersionTable
,因为您可能没有打开(我想),3或4。为清楚起见:OP表示,
测试连接
并非无处不在。它是用PS v2引入的。我同意
测试网络连接
是较新的(在v4中引入)。这让我们想知道为什么OP想要使用后者而不是前者,因为他担心兼容性。我正在研究Windows物联网。它缺少几个cmdlet。请告诉我,但对询问者来说,这是一个很好的解释和彻底的分解。根据这个答案,这是对我来说最好的(也是最容易阅读的)
(测试NetConnection 8.8.8.8)。pingreplydetails |选择Roundtriptime
太棒了!所以它就像一个属性的子属性?为了与我最初的操作方式保持一致,您可以得到如下正确的输出:
testnetconnection 8.8.8.8 | select-ExpandProperty PingReplyDetails | select RoundtripTime
RoundtripTime
-
29
我故意这样回答,因此,您可以在分配任务后尝试执行
$data
(自行执行)。在那里,您将看到RTT有更多的属性需要深入挖掘,是的。;)