PowerShell Test-Connection: モダンなPingテスト

今日のコマンドレットはPowerShellのTest-Connectionです。Test-Connectionは、あなたのネットワーク接続をテストするためのコマンドレットです。Test-Connectionは、人気のあるpingユーティリティのPowerShellによる実装と言えます。ICMPを共有しているため、表面的には似ていますが、内部では少し異なる方法で処理されていることがわかります。

このコマンドレットの使用は簡単です。基本的には、ComputerNameパラメータを指定するだけで、宛先ホストに4つの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パラメータで認証タイプを変更することもできます。

バックグラウンドジョブの使用

このコマンドレットはバックグラウンドジョブとしても実行できます。バックグラウンドジョブは、タイムアウトする可能性のある多数のリモートコンピュータに対して待ち続ける代わりに、ジョブをバックグラウンドで実行する際に便利です。

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のヘルプコンテンツを確認するか、Microsoftのドキュメントを参照して、詳細を確認してください。

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