PowerShell Test-Connection: 现代 Ping 测试

今天的指令集是 PowerShell Test-Connection。 Test-Connection 是一个测试网络连接的指令集。将 Test-Connection 视为 PowerShell 对流行的 ping 实用程序的实现是没有错的。尽管两者都使用 ICMP,但你会发现这两种方法在底层有一些不同。

使用这个指令集很简单。在最基本的情况下,只需指定一个 ComputerName 参数,它将向目标主机发送四个 ICMP 请求。

PS> Test-Connection -ComputerName google.com
Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms)
------        -----------     -----------      -----------                              -----    --------
MACWINVM      google.com      172.217.0.14     2607:f8b0:4009:80c::200e                 32       47
MACWINVM      google.com      172.217.0.14     2607:f8b0:4009:80c::200e                 32       90
MACWINVM      google.com      172.217.0.14     2607:f8b0:4009:80c::200e                 32       88
MACWINVM      google.com      172.217.0.14     2607:f8b0:4009:80c::200e                 32       205

这个输出看起来与 ping.exe 相似,表面上是这样的,但 Powershell test-connection 以稍微不同的方式发出 ICMP 请求。与 ping.exe 不同,Test-Connection 使用本地计算机的 WMI 类 Win32_PingStatus 发送 ICMP 请求。使用本地 WMI 存储库意味着你必须确保你的 本地 WMI 存储库正常,否则 Test-Connection 将无法工作。

PowerShell Test-Connection 的对象输出

此外,就像使用 PowerShell 一样,这个指令集不仅仅返回在控制台上显示的内容。我们可以看到丰富的对象,我们可以从中获取更多信息。

如果我将输出分配给一个变量,然后查看属性,你可以看到我收集了更多有用的信息。

PS> $pingResults | Get-Member
TypeName: System.Management.ManagementObject#root\cimv2\Win32_PingStatus
Name                           MemberType     Definition
----                           ----------     ----------
PSComputerName                 AliasProperty  PSComputerName = __SERVER
Address                        Property       string Address {get;set;}
BufferSize                     Property       uint32 BufferSize {get;set;}
NoFragmentation                Property       bool NoFragmentation {get;set;}
PrimaryAddressResolutionStatus Property       uint32
PrimaryAddressResolutionStatus {get;set;}
ProtocolAddress                Property       string ProtocolAddress {get;set;}
ProtocolAddressResolved        Property       string
ProtocolAddressResolved {get;set;}
RecordRoute                    Property       uint32 RecordRoute {get;set;}
ReplyInconsistency             Property       bool ReplyInconsistency {get;set;}
ReplySize                      Property       uint32 ReplySize {get;set;}
ResolveAddressNames            Property       bool ResolveAddressNames {get;set;}
ResponseTime                   Property       uint32 ResponseTime {get;set;}
ResponseTimeToLive             Property       uint32 ResponseTimeToLive {get;set;}
RouteRecord                    Property       string[] RouteRecord {get;set;}
RouteRecordResolved            Property       string[] RouteRecordResolved {get;set;}
SourceRoute                    Property       string SourceRoute {get;set;}
SourceRouteType                Property       uint32 SourceRouteType {get;set;}
StatusCode                     Property       uint32 StatusCode {get;set;}
Timeout                        Property       uint32 Timeout {get;set;}
TimeStampRecord                Property       uint32[] TimeStampRecord {get;set;}
TimeStampRecordAddress         Property       string[] TimeStampRecordAddress {get;set;} TimeStampRecordAddressResolved Property       string[]
TimeStampRecordAddressResolved {get;set;}
TimestampRoute                 Property       uint32 TimestampRoute {get;set;}
<SNIP>

如果您正在測試內部主機,Test-Connection會使用DCOM來對遠程主機進行身份驗證。默認情況下,它將使用封包級DCOM身份驗證,但身份驗證類型可以通過DcomAuthentication參數進行更改。

使用背景作業

此命令也可以作為後台作業運行。如果您有很多遠程計算機要ping,而不是永遠等待最終超時的計算機,只需將其發送到後台作業即可。

根據PowerShell test-connections的幫助,它說在本地和遠程計算機上都必須啟用PowerShell遠程,但這是不正確的。如下所示,我正在測試google.com,命令仍然運行良好。

Test-Connection -AsJob -ComputerName google.com
Get-Job | Receive-Job

保持簡單

最後,如果您只是想要一個二進制的是/否答案來判斷計算機是否有響應,您始終可以使用Quiet參數。我經常使用的一個常見字符串是使用QuietCount為1,以強制Test-Connection發送一個單獨的ICMP請求來快速查看服務器是否在線。

PS> Test-Connection -ComputerName google.com -Quiet -Count 1
True

這就是我們的每日 cmdlet!我們介紹了 Powershell test-connection 的大部分功能,但是像往常一樣,請查看 PowerShell 幫助內容或前往 Microsoft 說明文件 以獲取完整的詳細資訊。

Source:
https://adamtheautomator.com/powershell-test-connection/