在Power Shell中使用.NET属性

时间:2016-06-24 17:42:42

标签: .net powershell

我尝试使用Test-NetConnection测量延迟。我已经通过Test-Connection很好地使用它,但它并不是随处可用。

以下是我使用Test-Connection所做的事情:

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

如何从命令中获取实际值?

2 个答案:

答案 0 :(得分:3)

问题是PingReplyDetails (RTT)不是一个真正的属性,你可以看到下面的命令及其输出,其中缺少这个'property'。

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

答案 1 :(得分:1)

试试这个...我已将其分解为两个步骤,以便更加清晰,但如果您愿意,可以自行合并为一个语句。

$data = Test-NetConnection 8.8.8.8 | select -ExpandProperty PingReplyDetails
$data.RoundtripTime

看起来像我一样为我工作。由于您的问题包含不同的尝试,我相信您应该能够自定义以满足您的确切需求。

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