Ping Servers with PowerShell

本文分享了一个使用PowerShell批量检查服务器在线状态的脚本,支持循环运行、仅显示在线或离线服务器等功能。通过调用WMI的ping状态,可以高效地管理和监控大量服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里给大家分享一个在工作中写的小脚本,原理很简单。

在我的工作中时常有大批服务器需要操作管理,当你从线上拿下几台server做处理操作后,就需要时刻监视server的在线状态,下面我写了一个ping server list的脚本,该脚本支持添加loop、online、offline参数。最后还会显示ping server list所花费的时间。

 

参数说明:

[loop]循环运行ping脚本

[online]只显示ping通的server

[offline]只显示未ping通的server

[loop][online]以循环模式显示ping通的server

[loop][offline]以循环模式显示未ping通的server

具体说明随后会演示。

 

脚本的核心十分的简单,调用了WMI的ping状态来判断,最后结合参数配合调用。运行的时候输入保存的server list文本地址就可以批量ping server了。

 

<#
.SYNOPSIS 
Adds a file name extension to a supplied name.
.DESCRIPTION
Adds some parameters. 
.PARAMETER Extension
Specifies the ping mode.
.INPUTS
None. You cannot pipe objects to ping.ps1.
.OUTPUTS
System.String. ping.ps1 returns a string with the extension or file name.
.EXAMPLE
C:\PS> C:\ping.ps1
DEMOSER01    Online!
DEMOSER02    Offline!
DEMOSER03    Online!
.EXAMPLE
C:\PS> C:\ping.ps1 -loop
DEMOSER01    Online!
DEMOSER02    Offline!
DEMOSER03    Online!
DEMOSER01    Online!
DEMOSER02    Offline!
DEMOSER03    Online!
...........................
.EXAMPLE
C:\PS> C:\ping.ps1 -online
DEMOSER01    Online!
DEMOSER03    Online!
.EXAMPLE
C:\PS> C:\ping.ps1 -offline
DEMOSER02    Offline!
.LINK
Contact: anders@wenov.com
#>
			
param(
[switch]$online,
[switch]$offline,
[switch]$loop
)

$filepath = Read-Host "Please enter the file location"
$ComPList =  Get-Content "$filepath"

function global:Ping_Test
{   
	<#
	The Process statement list runs one time for each object in the pipeline.
	While the Process block is running, each pipeline object is assigned to 
	the $_ automatic variable, one pipeline object at a time. 
	#>
	PROCESS
    {
		$results = Get-WmiObject -query "SELECT * FROM Win32_PingStatus WHERE Address = '$_'"
#      $RT = $results.ResponseTime
#      $TTL = $results.ResponseTimeToLive

		if ($results.StatusCode -eq 0)
		{
			if(($online -eq $true -and $offline -ne $true) -or ($online -ne $true -and $offline -ne $true))
			{Write-Host "$Server  	Online!" -ForegroundColor Green}
		}
		else
		{
			if(($online -ne $true -and $offline -eq $true) -or ($online -ne $true -and $offline -ne $true))
			{Write-Host "$Server  	Offline!" -ForegroundColor Red}
		}
	}
}

#call procedure output results
do
{	
	$time=Measure-Command{
	foreach ($Server in $ComPList)
	{
  		$Server | Ping_Test
	}
	}
	Write-Host ">>>>>>>> SpendTime:"$time" <<<<<<<<<"  #add "" for $time variable can convert format to time.
}
while($loop) # if Switch Parameters are active the statement block cycling


 

运行界面如下(因为出于安全,我将机器名作了些涂抹):

 

 

添加loop参数,开启循环ping主机,直到按下中断组合键CTRL+C才会停止

 

添加online参数后只会显示ping通的主机

 

添加offline参数则只显示ping不通的主机

 

另外也可以使用组合参数loop和offline,脚本将会循环ping server,并且只显示ping不通的主机,直到按下CTRL+C中断组合键停止

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值