简体   繁体   English

Powershell 用于 ping 服务器的脚本

[英]Powershell script for pinging servers

I'd like to write a powershell script for pinging servers.我想为 ping 服务器编写一个 powershell 脚本。 The script would call.ini file which contains a list of servers to be pinged.该脚本将调用包含要 ping 的服务器列表的 .ini 文件。

Could you please advise how to do this?你能告诉我怎么做吗?

If it is just a list of server ip addresses then the following would work.如果它只是服务器 ip 地址的列表,那么以下将起作用。

Get-Content "c:\testvalues.ini" | foreach { Test-Connection $_ }

This works well in powershell 5 to ping in parallel:这在 powershell 5 中运行良好,可以并行 ping:

$list = get-content list.ini
test-connection $list -AsJob -count 1 | Receive-Job -Wait -AutoRemoveJob

If a computer is down, the column under the header "Time(ms)" will be blank, or the ResponseTime property will be $null.如果计算机出现故障,则 header“时间(毫秒)”下的列将为空,或者 ResponseTime 属性将为 $null。

I prepared a small PowerShell script which will help to ping the windows/Linux hosts via tcp ping.我准备了一个小的 PowerShell 脚本,这将有助于通过 tcp ping ping windows/Linux 主机。

In this script I pinged Linux host using TCP port 22 from my windows desktop.在这个脚本中,我使用 windows 桌面的 TCP 端口 22 ping Linux 主机。

You can modify the port number as per your convenient in the script itself.您可以根据自己的方便在脚本本身中修改端口号。

For Example:例如:

Linux = Port 22 (SSH TCP) Linux = 端口 22 (SSH TCP)

.\paping.exe $line -p 22 -c 1

Windows = Port 3389 (RDP TCP) Windows = 端口 3389 (RDP TCP)

.\paping.exe $line -p 3389 -c 1

Step 1: Download " paping " network utility from the below URL.步骤 1:从下面的 URL 下载“ paping ”网络实用程序。

https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/paping/paping_1.5.5_x86_windows.zip https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/paping/paping_1.5.5_x86_windows.zip

Step 2: Extract the downloaded zip file in your desktop.第 2 步:将下载的 zip 文件提取到您的桌面。

Step 3: Create a text file name as "ips.txt" and store in to your desktop under "paping_1.5.5_x86_windows" folder.第 3 步:创建一个文本文件名为“ips.txt”,并存储到桌面的“paping_1.5.5_x86_windows”文件夹下。

Step 4: Copy the below script and save into the PowerShell name as "tcpping.ps1"第 4 步:复制以下脚本并保存到 PowerShell 名称为“tcpping.ps1”

Clear-Host
Clear-Content .\success.log -Force -ErrorAction SilentlyContinue
Clear-Content .\failure.log -Force -ErrorAction SilentlyContinue 
foreach($line in Get-Content .\ips.txt) 

{
.\paping.exe $line -p 22 -c 1
if ( $? -eq $True )

{
   $currentTime = Get-Date -Format “MM/dd/yyyy HH:mm K”
    Write-Host $currentTime Node $line is up
    
    "$(Get-Date -DisplayHint datetime) Node $line is up" | Tee-Object -FilePath "success.log" -Append

}

else
{
$currentTime = Get-Date -Format “MM/dd/yyyy HH:mm K”
 Write-Host $currentTime Node $line is down
 "$(Get-Date -DisplayHint datetime) Node $line is down" | Tee-Object -FilePath "failure.log" -Append

 }

}

Step 5: Right click PowerShell script and Run with PowerShell.第 5 步:右键单击 PowerShell 脚本并使用 PowerShell 运行。

Step 6: You can view the tcp ping success and failure results in the same folder log files.第六步:可以在同一个文件夹的日志文件中查看tcp ping成功和失败结果。

Step 7: Below is the sample ping test result script output for your reference第 7 步:下面是示例 ping 测试结果脚本 output 供您参考

07/18/2021 23:27:45 Node 192.168.0.10 is up
07/18/2021 23:27:47 Node 192.168.0.11 is down

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM