簡體   English   中英

在Power Shell中使用.NET屬性

[英]Working with .NET properties in Power Shell

我正在嘗試使用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

如何從命令中獲取實際值?

問題是, 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可以顯示對象中的詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM