Test-Connection
將ICMP回音要求封包(ping)傳送至一部或多部電腦。
語法
Test-Connection
[-TargetName] <string[]>
[-Ping]
[-IPv4]
[-IPv6]
[-ResolveDestination]
[-Source <string>]
[-MaxHops <int>]
[-Count <int>]
[-Delay <int>]
[-BufferSize <int>]
[-DontFragment]
[-Quiet]
[-TimeoutSeconds <int>]
[<CommonParameters>]
Test-Connection
[-TargetName] <string[]>
-Repeat
[-Ping]
[-IPv4]
[-IPv6]
[-ResolveDestination]
[-Source <string>]
[-MaxHops <int>]
[-Delay <int>]
[-BufferSize <int>]
[-DontFragment]
[-Quiet]
[-TimeoutSeconds <int>]
[<CommonParameters>]
Test-Connection
[-TargetName] <string[]>
-Traceroute
[-IPv4]
[-IPv6]
[-ResolveDestination]
[-Source <string>]
[-MaxHops <int>]
[-Quiet]
[-TimeoutSeconds <int>]
[<CommonParameters>]
Test-Connection
[-TargetName] <string[]>
-MtuSize
[-IPv4]
[-IPv6]
[-ResolveDestination]
[-Quiet]
[-TimeoutSeconds <int>]
[<CommonParameters>]
Test-Connection
[-TargetName] <string[]>
-TcpPort <int>
[-IPv4]
[-IPv6]
[-ResolveDestination]
[-Source <string>]
[-Count <int>]
[-Delay <int>]
[-Repeat]
[-Quiet]
[-TimeoutSeconds <int>]
[-Detailed]
[<CommonParameters>]
Description
Test-Connection
Cmdlet 會將 Internet 控制訊息協定(ICMP)回應要求封包或 Ping 包傳送至一或多部遠端電腦,並傳回回應結果。 您可以使用此 Cmdlet 來判斷是否可以透過 IP 網路連絡特定電腦。
您可以使用 Test-Connection
的參數來指定傳送和接收計算機、以背景工作方式執行命令、設定逾時和 ping 數目,以及設定連線和驗證。
不同於熟悉 ping 命令,Test-Connection
會傳回您可以在 PowerShell 中調查的 TestConnectionCommand+PingStatus 物件。
Quiet 參數會針對每個測試的連接傳回 System.Boolean 物件中的 布爾值 值。 如果測試了多個連線,則會傳回 布爾值陣列 值。
範例
範例 1:將回應要求傳送至遠端電腦
此範例會將本機電腦的迴聲請求封包傳送至 Server01 電腦。
Test-Connection -TargetName Server01 -IPv4
Destination: Server01
Ping Source Address Latency BufferSize Status
(ms) (B)
---- ------ ------- ------- ---------- ------
1 ADMIN1 10.59.137.44 24 32 Success
2 ADMIN1 10.59.137.44 39 32 Success
3 ADMIN1 * * * TimedOut
4 ADMIN1 10.59.137.44 28 32 Success
Test-Connection
會使用 TargetName 參數來指定 Server01 計算機。
IPv4 參數會指定測試的通訊協定。
一系列 TestConnectionCommand+PingStatus 物件會傳送至輸出流,每次從目標計算機接收到 Ping 回復時對應一個物件。
範例 2:將回應要求傳送至數部計算機
此範例會將 Ping 從本機電腦傳送到數部遠端電腦。
Test-Connection -TargetName Server01, Server02, Server12
範例 3:使用參數來客製化測試命令
此範例會使用 Test-Connection
的參數來自訂命令。 本地電腦會將 Ping 測試傳送至遠端電腦。
Test-Connection -TargetName Server01 -Count 3 -Delay 2 -MaxHops 255 -BufferSize 256
Test-Connection
會使用 TargetName 參數來指定 Server01。
Count 參數會指定三個 ping 會傳送至 Server01 電腦,且延遲 為 2 秒間隔。
當預期的 Ping 回應時間比平常長時,例如由於躍點數目較多或網路流量大的情況,您可能會使用這些選項。
範例 4:以背景工作身分執行測試
此範例示範如何將 Test-Connection
命令當做 PowerShell 背景工作來執行。
$job = Start-Job -ScriptBlock { Test-Connection -TargetName (Get-Content -Path "Servers.txt") }
$Results = Receive-Job $job -Wait
Start-Job
命令會使用 Test-Connection
Cmdlet 來偵測企業中的許多電腦。
TargetName 參數的值是 Get-Content
命令,會從 Servers.txt
檔案讀取電腦名稱清單。 命令會使用 Start-Job
Cmdlet 來執行命令作為背景工作,並將工作儲存在 $job
變數中。
系統會指示 Receive-Job
命令執行 -Wait
直到工作完成,然後取得結果,並將其儲存在 $Results
變數中。
範例 5:只有在連線測試成功時,才建立會話
只有在至少一個傳送到計算機的 Ping 成功時,這個範例才會在 Server01 計算機上建立會話。
if (Test-Connection -TargetName Server01 -Quiet) { New-PSSession -ComputerName Server01 }
Test-Connection
Cmdlet 會偵測 Server01
計算機,並提供 Quiet 參數。
如果四個 ping 中的任何一個成功,產生的值就會 $True
。 如果沒有任何 ping 成功,則值會是 $False
。
如果 Test-Connection
命令傳回 $True
值,命令會使用 New-PSSession
Cmdlet 來建立 PSSession。
範例 6:使用 Traceroute 參數
在 PowerShell 6.0 中引入的 Traceroute 參數,用於映射本機電腦與通過 TargetName 參數指定的遠端目的地之間的路由。
Test-Connection -TargetName www.google.com -Traceroute
Target: google.com
Hop Hostname Ping Latency Status Source TargetAddress
(ms)
--- -------- ---- ------- ------ ------ -------------
1 172.20.0.1 1 4 Success Lira 172.217.9.174
1 172.20.0.1 2 3 Success Lira 172.217.9.174
1 172.20.0.1 3 2 Success Lira 172.217.9.174
2 12.108.153.193 1 3 Success Lira 172.217.9.174
2 12.108.153.193 2 3 Success Lira 172.217.9.174
2 12.108.153.193 3 2 Success Lira 172.217.9.174
3 12.244.85.177 1 11 Success Lira 172.217.9.174
3 12.244.85.177 2 12 Success Lira 172.217.9.174
3 12.244.85.177 3 12 Success Lira 172.217.9.174
4 * 1 14 DestinationNetw… Lira 172.217.9.174
4 * 2 * TimedOut Lira 172.217.9.174
4 * 3 20 DestinationNetw… Lira 172.217.9.174
5 * 1 * TimedOut Lira 172.217.9.174
5 * 2 15 DestinationNetw… Lira 172.217.9.174
5 * 3 * TimedOut Lira 172.217.9.174
6 * 1 18 DestinationNetw… Lira 172.217.9.174
6 * 2 * TimedOut Lira 172.217.9.174
6 * 3 16 DestinationNetw… Lira 172.217.9.174
7 * 1 * TimedOut Lira 172.217.9.174
7 * 2 * TimedOut Lira 172.217.9.174
7 * 3 * TimedOut Lira 172.217.9.174
8 * 1 * TimedOut Lira 172.217.9.174
8 * 2 * TimedOut Lira 172.217.9.174
8 * 3 * TimedOut Lira 172.217.9.174
9 * 1 * TimedOut Lira 172.217.9.174
9 * 2 * TimedOut Lira 172.217.9.174
9 * 3 * TimedOut Lira 172.217.9.174
10 * 1 * TimedOut Lira 172.217.9.174
10 * 2 * TimedOut Lira 172.217.9.174
10 * 3 * TimedOut Lira 172.217.9.174
11 172.217.9.174 1 23 Success Lira 172.217.9.174
11 172.217.9.174 2 21 Success Lira 172.217.9.174
11 172.217.9.174 3 22 Success Lira 172.217.9.174
使用 Test-Connection
參數呼叫 命令。 結果是 [Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus]
物件,會輸出至 Success 輸出流。
範例 7:取得 TCP 連線測試的詳細輸出
當您使用 Detailed 參數時,此 Cmdlet 會傳回 TCP 連線嘗試狀態的詳細資訊。
Test-Connection bing.com -TCPPort 443 -Detailed -Count 4
Target: bing.com
Id Source Address Port Latency(ms) Connected Status
-- ------ ------- ---- ----------- --------- ------
1 circumflex 2620:1ec:c11::200 443 12 True Success
2 circumflex 2620:1ec:c11::200 443 14 True Success
3 circumflex 2620:1ec:c11::200 443 17 True Success
4 circumflex 2620:1ec:c11::200 443 17 True Success
參數
-BufferSize
指定使用此命令傳送之緩衝區的大小,以位元組為單位。 預設值為 32。
類型: | Int32 |
別名: | Size, Bytes, BS |
Position: | Named |
預設值: | 32 |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Count
指定要傳送的回應要求數目。 預設值為 4。
類型: | Int32 |
Position: | Named |
預設值: | 4 |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Delay
指定 ping 之間的間隔,以秒為單位。
類型: | Int32 |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Detailed
當您使用 Detailed 參數時,此 Cmdlet 會傳回 TCP 連線嘗試狀態的詳細資訊。
此參數已在PowerShell 7.4中新增。
類型: | SwitchParameter |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-DontFragment
此參數會在IP標頭中設定 不分段 旗標。 您可以使用此參數搭配 BufferSize 參數來測試 Path MTU 大小。 如需 Path MTU 的詳細資訊,請參閱維基百科中的 Path MTU 探索 文章。
類型: | SwitchParameter |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-IPv4
強制 Cmdlet 使用 IPv4 通訊協定進行測試。
類型: | SwitchParameter |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-IPv6
強制 Cmdlet 使用 IPv6 通訊協定進行測試。
類型: | SwitchParameter |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-MaxHops
設定 ICMP 請求訊息的最大可傳輸躍點數。 預設值是由操作系統所控制。 Windows 10 及以上版本的預設值為 128 個躍點。
類型: | Int32 |
別名: | Ttl, TimeToLive, Hops |
Position: | Named |
預設值: | 128 |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-MtuSize
此參數可用來探索路徑 MTU 大小。 Cmdlet 會傳回包含目標路徑 MTU 大小的 PingReply#MTUSize 物件。 如需 Path MTU 的詳細資訊,請參閱維基百科中的 Path MTU 探索 文章。
類型: | SwitchParameter |
別名: | MtuSizeDetect |
Position: | Named |
預設值: | False |
必要: | True |
接受管線輸入: | False |
接受萬用字元: | False |
-Ping
導致 Cmdlet 執行 Ping 測試。 這是 Test-Connection
Cmdlet 的預設模式。
類型: | SwitchParameter |
Position: | Named |
預設值: | True |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Quiet
Quiet 參數會傳回布爾值 值。 使用此參數會隱藏所有錯誤。
每個被測試的連接都會傳回 布爾值。 如果 TargetName 參數指定多部計算機,則會傳回 布爾值 值的陣列。
如果 指定目標的任何 ping 成功,則會傳回 $True
。
如果 對指定目標的所有 Ping 失敗,則會傳回 $False
。
類型: | SwitchParameter |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Repeat
導致 Cmdlet 持續傳送 Ping 要求。 當 TargetName 的值是目標陣列時,Cmdlet 只會針對第一個目標重複 ping 要求。 它會忽略其餘的目標。 此參數無法與 Count 參數搭配使用。
類型: | SwitchParameter |
別名: | Continuous |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-ResolveDestination
導致 Cmdlet 嘗試解析目標的 DNS 名稱。 搭配 Traceroute 參數使用時,也會盡可能擷取所有中繼主機的 DNS 名稱。
類型: | SwitchParameter |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Source
指定 Ping 來源的電腦名稱。 輸入以逗號分隔的計算機名稱清單。 預設值為本機電腦。
注意
PowerShell 第 6 版和更新版本中不支援此參數。 提供此參數會造成錯誤。
類型: | String |
Position: | Named |
預設值: | Local computer |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-TargetName
指定要測試的計算機。 以IPv4或IPv6格式輸入電腦名稱或輸入IP位址。
類型: | String[] |
別名: | ComputerName |
Position: | 0 |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
-TcpPort
指定要在 TCP 連線測試中使用的目標 TCP 通訊埠號碼。
Cmdlet 會嘗試對目標上的指定埠進行 TCP 連線。
- 如果建立連接,Cmdlet 會傳回
$True
。 - 如果未建立連線,Cmdlet 會傳回
$False
。
類型: | Int32 |
Position: | Named |
預設值: | None |
必要: | True |
接受管線輸入: | False |
接受萬用字元: | False |
-TimeoutSeconds
設定測試的逾時值。 如果逾時到期前未收到回應,測試就會失敗。 預設值為五秒。
此參數是在 PowerShell 6.0 中引進的。
類型: | Int32 |
Position: | Named |
預設值: | 5 seconds |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Traceroute
導致 Cmdlet 執行追蹤路由測試。 使用此參數時,Cmdlet 會傳回 TestConnectionCommand+TraceStatus
物件。
類型: | SwitchParameter |
Position: | Named |
預設值: | False |
必要: | True |
接受管線輸入: | False |
接受萬用字元: | False |
輸入
None
您無法使用管線將物件傳送至此 cmdlet。
輸出
Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus
根據預設,此 Cmdlet 會針對每個 Ping 回復傳回 TestConnectionCommand+PingStatus 物件。
Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus
當您使用 Traceroute 參數時,這個 Cmdlet 會針對路由上的每個 Ping 回覆返回 TestConnectionCommand+TraceStatus 物件。
當您使用 Quiet 或 TcpPort 參數時,此 Cmdlet 會傳回 布爾值 值。 如果 Cmdlet 測試多個連線,則會傳回布爾值 陣列 值。
Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus
當您使用 MtuSize 參數時,此 Cmdlet 會針對每個 Ping 回復傳回 TestConnectionCommand+PingMtuStatus 物件。
Microsoft.PowerShell.Commands.TestConnectionCommand+TcpPortStatus
當您使用 Detailed 參數時,此 Cmdlet 會傳回 TestConnectionCommand+TcpPortStatus 物件,該對象會顯示 TCP 連線的狀態。
備註
在 Linux 上,使用 BufferSize 參數或任何參數組合搭配 MtuSize 參數集,可能會導致非預設緩衝區大小為 32 個字節,可能需要 sudo
。 在這些情況下,Test-Command
會引發例外狀況,並顯示需要 sudo
的訊息。