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

[玩转系统] 测试连接:以 PowerShell 方式 Ping 远程主机

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

测试连接:以 PowerShell 方式 Ping 远程主机


今天的 cmdlet 是 PowerShell Test-Connection。 Test-Connection 是一个 cmdlet,它毫不奇怪地测试您的网络连接。将 Test-Connection 视为流行 ping 实用程序的 PowerShell 实现。尽管两者都有 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 测试连接发出的 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 帮助,它说必须在本地和远程计算机上启用 PowerShell 远程处理,但事实并非如此。如下所示,我正在测试 google.com,该命令仍然运行良好。

[玩转系统] 测试连接:以 PowerShell 方式 Ping 远程主机

[玩转系统] 测试连接:以 PowerShell 方式 Ping 远程主机

保持简单

最后,如果您只是寻找计算机是否响应的二进制是/否答案,则始终可以使用 Quiet 参数。我经常用来快速查看服务器是否在线的常用字符串是使用 QuietCount of 1 来强制 Test-Connection发送单个 ICMP 请求。

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

这就是我们今天的 cmdlet!我们介绍了 Powershell 测试连接的大部分功能,但与往常一样,请查看 PowerShell 帮助内容或访问 Microsoft 文档以获取完整的详细信息。

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

取消回复欢迎 发表评论:

关灯