PowerShell Test-Connection:现代Ping测试

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

使用这个cmdlet很简单。在其最基本的用法中,只需指定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的美妙之处一样,这个cmdlet不仅仅返回在控制台上立即显示的内容。我们看到了我们可以从中收集更多信息的丰富对象。

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

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 参数进行更改。

使用后台作业

此 cmdlet 也可以作为后台作业运行。如果您要 ping 大量远程计算机,而不是永远等待最终会超时的计算机,只需将其发送到后台作业即可。

根据 PowerShell 帮助中的 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 帮助内容,或者转到 微软文档 以获取完整的详细信息。

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