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

[玩转系统] PowerShell - 测试连接到 Ping 计算机列表

作者:精品下载站 日期:2024-12-14 22:01:45 浏览:13 分类:玩电脑

PowerShell - 测试连接到 Ping 计算机列表


使用 PowerShell Test-Connection cmdlet 检查多台计算机的活动状态(是否在线或离线),轻松确定计算机列表是否可达。

在本文中,我们将讨论如何在 PowerShell 中使用 PowerShell 测试连接 ping 计算机列表。您需要使用 PowerShell ping 命令来测试计算机的回显或响应。

测试连接 cmdlet

PowerShell Test-Connection cmdlet 发送 ICMP 回显请求数据包或 ping 一台或多台远程计算机并返回回显响应答复

句法

Test-Connection
    [-AsJob]
    [-DcomAuthentication <AuthenticationLevel>]
    [-WsmanAuthentication <String>]
    [-Protocol <String>]
    [-BufferSize <Int32>]
    [-ComputerName] <String[]>
    [-Count <Int32>]
    [-Impersonation <ImpersonationLevel>]
    [-ThrottleLimit <Int32>]
    [-TimeToLive <Int32>]
    [-Delay <Int32>]
    [<CommonParameters>]

测试连接以 ping 多台计算机

让我们考虑一个例子,我们有一个包含计算机列表的 txt 文件。

我们需要 ping 远程计算机列表以了解其状态并将其响应作为输出。

使用下面的 PowerShell ping 脚本测试计算机列表的连接状态

$complist = Get-Content "D:\PowerShell\complist.txt"

foreach($comp in $complist){
    
    $pingtest = Test-Connection -ComputerName $comp -Quiet -Count 1 -ErrorAction SilentlyContinue

    if($pingtest){

         Write-Host($comp + " is online")
     }
     else{
        Write-Host($comp + " is not reachable")
     }
     
}

输出

incorp-as-P517 is online
incorp-as-627 is not reachable

在上面的PowerShell 测试连接脚本中,我们将计算机列表存储在文本文件中。

使用 Get-Content cmdlet,它读取 txt 文件并将列表存储到 $complist 变量中。

由于我们必须 ping 远程计算机列表,因此使用 ForEach 循环一次迭代每台计算机名称。

使用 test-connection cmdlet 发送回显请求或 ping。

-Count 1 参数与 test-connection cmdlet 可确保仅 ping 远程主机一次并获得响应。

-ErrorAction SilentlyContinue 参数确保在主机没有响应的情况下,不会因主机离线而抛出任何错误并打印输出。

如果 test-connection ping 状态为 true,则使用 Write-Host cmdlet 在输出上打印响应,因为“计算机名称”在线,否则“计算机名称离线”。

酷提示:你知道如何在 PowerShell 中打印环境变量吗?

让我们看看其他一些 PowerShell 测试连接示例和脚本,以使用 PowerShell ping 脚本ping 多台计算机

PowerShell 测试连接示例

Ping 主机名列表并将其导出到 CSV 文件

让我们考虑一下,您有一个巨大的主机名列表,并且您想测试主机名列表的连接是否在线或离线。

使用下面的 PowerShell ping 脚本,您可以轻松 ping 主机名列表并将结果输出到 CSV 文件

$outputcsv = "D:\PowerShell\pingstatus.csv"
$hostlist = Get-Content "D:\PowerShell\hostlist.txt"
$OutputMessage = @()

foreach($comp in $hostlist){
    
    $pingtest = Test-Connection -ComputerName $comp -Quiet -Count 1 -ErrorAction SilentlyContinue

    if($pingtest) {
         $OutputMessage += "$comp,Online"
      }
     else {
   
       $OutputMessage += "$comp,Offline"
          
     }
     
}

 $OutputMessage | Out-File $outputcsv -Encoding utf8 -Append

在上面的 PowerShell ping 脚本中,使用 Get-Content 读取主机名列表,并使用 ForEach 循环迭代每个主机名。

使用 test-connection 对计算机列表中的每台计算机执行 ping 操作。

如果 test-connection ping status true 则将计算机名称和 ping 状态存储为在线 $OuputMessage 变量,否则将计算机名称和 ping 状态存储为离线 $OutputMessage

$OuputMessage 通过 Out-File 管道将结果输出到文件并导出到 CSV。

酷提示:您知道 Windows 中等效的 cat 命令吗?

PowerShell Test-Connection 向远程计算机系统发送回显请求?

使用 PowerShell test-connection cmdlet,它会向远程计算机发送 ICMP 回显请求。

>Test-Connection -ComputerName mycomp-517
#Ouput
Source  Destination IPV4Address  IPV6Address  Bytes    Time(ms) 
------  ----------- -----------  -----------  -----    -------- 
CORP-56 comp-517  10.217.166.17                32       7        
CORP-56 comp-517  10.217.166.17                32       8        
CORP-56 comp-517  10.217.166.17                32       18       
CORP-56 comp-517  10.217.166.17                32       9  

在上面的 PowerShell 测试连接示例中,该命令使用 PowerShell ping Test-Connection cmdlet 中指定的 ComputerName 参数向 mycomp-517 发送回显请求。

使用 Test-Connection 将多台计算机的回显请求发送到一台计算机

下面的示例将回显请求从多台计算机发送到一台远程计算机。

Test-Connection -Source corp-25, corp-server-20, corp-server-23 -ComputerName corp-server-10 -Credential shellgeek\shelladmin

在上面的示例中,test-connection PowerShell cmdlet 将来自多台计算机 corp-25、corp-server-20 和 corp-server-23 的回显请求发送到单个远程计算机 corp-server-10,以测试来自不同的地方。

酷提示: Get-AdComputer - 通过示例查找 OU 中的计算机详细信息!

使用自定义参数测试远程计算机的连接

下面的示例使用 PowerShell ping test-connection 参数来测试远程计算机。

Test-Connection -ComputerName corp-server-25 -Count 2 -Delay 2 -TTL 255 -BufferSize 256 -ThrottleLimit 32

在上面的示例中,test-connection 使用ComputerName 参数指定 corp-server-25。它使用 Count 参数发送回显请求 2 次,Delay 间隔为 2 秒。

如何将 test-connection 命令作为 PowerShell 后台作业运行?

$job = Test-Connection -ComputerName (Get-Content hostlist.txt) -AsJob

if ($job.JobStateInfo.State -ne "Running") {$Results = Receive-Job $job}

在上面的 PowerShell 测试连接示例中,hostlist.txt 包含远程主机列表。

使用 Get-Content cmdlet,它读取所有远程计算机名称。 test-connection cmdlet 参数 ComputerName 从列表中获取主机名。

PowerShell ping test-connection cmdlet 使用 AsJob 参数对远程计算机列表运行 ping 请求作为后台作业并保存后台作业在 $job 中。

该命令检查作业是否处于运行状态。如果作业未处于运行状态,Receive-Job 将返回后台作业的结果并存储结果。

酷提示:您知道如何在 PowerShell 中下载 zip 文件吗?

结论

在上面的文章中,我们看到了使用 PowerShell test-connection 来 ping 计算机列表、发送回显请求、测试计算机列表的连接状态以及用于 ping 多台计算机的脚本的多个示例。

Test-Connection cmdlet 还用于在 PowerShell 中执行 ping 扫描。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

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

取消回复欢迎 发表评论:

关灯