当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何在 PowerShell 中使用 Test-NetConnection

作者:精品下载站 日期:2024-12-14 03:40:37 浏览:16 分类:玩电脑

如何在 PowerShell 中使用 Test-NetConnection


您是否知道 ping 命令已有 38 年历史,比命令提示符还要古老?您可能已经用 PowerShell 替换了日常工具集中的命令提示符。因此,现在是时候用 PowerShell Test-NetConnection 替换 ping 命令了。

ping 的优点是输入的命令非常短,但缺点是它仅测试通过 ICMP 的网络连接。它不进行任何跟踪或端口测试。大多数时候,我们需要将 ping 命令与 Tracert 和 nslookup 结合起来,以获取我们需要的所有详细信息。

Test-NetConnection 是 PowerShell Test-Connection cmdlet 的后继者。后者是取代 ping 命令的第一步,具有一些优点。 Test-NetConnection 功能更强大,应该是解决网络问题时的首选工具。

PowerShell 测试连接

在了解如何在 PowerShell 中使用 Test-NetConnection cmdlet 之前,让我们先快速浏览一下 Test-Connection

就像 ping 一样,使用 Test-Connection 也是 ICMP 协议来测试网络设备的网络连接性。在最简单的形式中,您可以键入 Test-Connection 来执行快速连接测试。

# Check connection on IP Address
Test-Connection 8.8.8.8

Source        Destination     IPV4Address      Bytes    Time(ms)
------        -----------     -----------      -------  ----
lab01         8.8.8.8         8.8.8.8          32       12
lab01         8.8.8.8         8.8.8.8          32       13
lab01         8.8.8.8         8.8.8.8          32       12
lab01         8.8.8.8         8.8.8.8          32       12

# Check connection on DNS Name
Test-Connection google.com

但我们当然可以做得更多。例如,我们可以使用一个命令测试多个目的地:

Test-Connection -ComputerName 8.8.8.8, 1.1.1.1

[玩转系统] 如何在 PowerShell 中使用 Test-NetConnection

或者指定跳数、缓冲区大小等参数,甚至在 ping 之间添加延迟:

Test-Connection -ComputerName 8.8.8.8 -Count 3 -Delay 2 -MaxHops 255 -BufferSize 256

测试远程计算机

我们还可以使用 Test-Connection 在远程计算机的 PowerShell 中测试网络连接。唯一的要求是您的帐户有权访问远程计算机。

Test-Connection -Source srv-lab02 -ComputerName 8.8.8.8

您还可以键入 tnc 而不是 Test-NetConnecting。

例如,您可以输入:tnc 8.8.8.8 来 ping Google。

PowerShell 测试与服务器的连接

PowerShell Test-Connection 的另一个有用功能是它可以返回 $true$false。这允许您在连接到计算机之前检查计算机是否可用。如果 4 个 ping 中的任何一个成功,Test-Connect 将返回 $true

if (Test-Connection -TargetName srv-lab02 -Quiet) { New-PSSession -ComputerName srv-lab02}

PowerShell 测试-NetConnection

Test-NetConnection cmdlet 是 Test-Connection 的后继者,因此基本功能都是相同的,只是输出更详细。我们仍然可以对 Google 进行快速 ping:

# Test the network connection to Google
Test-NetConnection 8.8.8.8

ComputerName           : 8.8.8.8
RemoteAddress          : 8.8.8.8
InterfaceAlias         : Wi-Fi
SourceAddress          : 192.168.1.82
PingSucceeded          : True
PingReplyDetails (RTT) : 9 ms

正如您所看到的,结果更加详细。它将显示所使用的接口、源 IP 地址以及 ping 是否成功。我们甚至可以通过添加 informationLevel "Detailed" 开关来扩展它。

informationLevel设置为detailed后,它基本上会对目标地址进行nslookup,并在查找中添加第一跳。

test-netconnection Google.com -InformationLevel "Detailed"                                                                                                                                                                                                                                                                                                            ComputerName           : Google.com                                                                                     RemoteAddress          : 172.217.17.78                                                                                  NameResolutionResults  : 172.217.17.78                                                                                  InterfaceAlias         : Wi-Fi                                                                                          SourceAddress          : 192.168.1.82                                                                                   NetRoute (NextHop)     : 192.168.1.1                                                                                    PingSucceeded          : True                                                                                           PingReplyDetails (RTT) : 6 ms

[玩转系统] 如何在 PowerShell 中使用 Test-NetConnection

当您运行不带任何参数的 Test-NetConnection 时,您可以在 PowerShell 中执行快速网络连接测试。这样我们就可以检查是否已连接到本地网络、是否可以访问互联网以及是否能够解析 DNS 名称。

❯ Test-NetConnection

ComputerName           : internetbeacon.msedge.net
RemoteAddress          : 13.107.4.52
InterfaceAlias         : Wi-Fi
SourceAddress          : 192.168.1.82
PingSucceeded          : True
PingReplyDetails (RTT) : 10 ms

带有 Test-NetConnection 的 PowerShell TraceRoute

通过 PowerShell 中的 Test-NetConnection cmdlet,我们还可以执行traceroute。您只需将 traceroute 参数添加到 cmdlet 即可。

# PowerShell TraceRoute with Test-NetConnection
Test-NetConnection 172.217.17.87 -traceRoute

[玩转系统] 如何在 PowerShell 中使用 Test-NetConnection

将 Traceroute 参数添加到 PowerShell Test-NetConnection cmdlet 后,您将获得 ping 期间传递的每个跃点的列表。您可以使用 hops 参数限制要跟踪的跃点数量。

要测试每一跳的延迟,可以选择TraceRoute对象,并测试每一跳的连接:

Test-NetConnection 172.217.17.78 -traceRoute -Hops 3 | select-object TraceRoute | foreach-object {test-connection $_.TraceRoute -count 1}

Powershell端口扫描

在 Windows 中,我们无法真正使用 ping cmdlet 来 ping 端口。我们可以使用 telnet 来测试端口是否响应,但使用 Test-NetConnection 我们可以更轻松地在 PowerShell 中扫描端口。

我们可以定义要测试的任何 TCP 端口,或使用常见端口 HTTP、RDP、SMB 或 WINRM 之一。

# Test if HTTP port is open
Test-NetConnection google.com -CommonTCPPort "Http"

# Or define a port number
Test-NetConnection google.com -Port 80

这样,我们就可以创建一个简单的 PowerShell 端口扫描脚本来检查服务器上的开放端口。下面的脚本旨在让您了解如何使用 PowerShell 扫描多个端口。

$ports = 22,53,80,445

$ports | ForEach-Object {$port = $_; if (Test-NetConnection -ComputerName 8.8.8.8 -Port $port -InformationLevel Quiet -WarningAction SilentlyContinue) {"Port $port is open" } else {"Port $port is closed"} }

总结

正如您所看到的,PowerShell Test-NetConnection 是一个非常强大且有用的 cmdlet,可在您的日常工作中使用。在文章开头,我提到 ping 的输入时间更短。您可以通过在 PowerShell 配置文件中创建别名或使用内置别名 tnc 来解决此问题。

# Create an alias "nettest" or maybe just "tn"
Set-Alias nettest Test-NetConnection

这样您就可以简单地键入“nettest”而不是 Test-NetConnection。在本文中阅读有关设置 PowerShell 配置文件的更多信息。

如果您有任何疑问,请在下面发表评论。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯