Powershell(Windows服务器的服务检查)

这个脚本可用于Windows服务器的日常检查、维护。它可以检查服务的状态,并且生成一个html格式的报告。

(1)powershell脚本的代码:

$ServerList = Get-Content "D:\st07\windows\shell\shell_1\servers.txt" 
$ServicesList = Get-Content "D:\st07\windows\shell\shell_1\services.txt"
$report = "D:\st07\windows\shell\shell_1\report.htm"
Add-Content $report "<html>"  
Add-Content $report "<head>"  
Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>"  
Add-Content $report '<title> Service Status </title>'  
Add-content $report '<style type="text/css">'  
Add-content $report  "<!--"  
Add-content $report  "td {"  
Add-content $report  "font-family: 宋体;"  
Add-content $report  "font-size: 20px;"   
Add-content $report  "}"  
Add-content $report  "body {"  
Add-content $report  "margin-left: 5px;"  
Add-content $report  "margin-top: 5px;"    
Add-content $report  "margin-bottom: 10px;"  
Add-content $report  "}"    
Add-content $report  "-->"  
Add-content $report  "</style>"  
Add-Content $report "</head>"  
Add-Content $report "<body>"  
Add-content $report  "<table width='100%'>"  
Add-content $report  "<tr bgcolor='green'>"  
Add-content $report  "<td colspan='7' height='28' align='center'>"  
Add-content $report  "<font face='宋体' size='5'> Service Status  </font>"  
Add-content $report  "</td>"  
Add-content $report  "</tr>"  
Add-content $report  "</table>"  
Add-content $report  "<table width='100%'>"  
Add-Content $report "<tr bgcolor='green'>"  
Add-Content $report  "<td width='10%' align='center'> server </td>"  
Add-Content $report "<td width='50%' align='center'> service </td>"  
Add-Content $report  "<td width='10%' align='center'> status </td>"  
Add-Content $report "</tr>"  
 
Function servicestatus ($serverlist, $serviceslist){ 
	foreach ($machineName in $serverlist){  
		foreach ($service in $serviceslist) { 
			$serviceStatus = get-service -ComputerName $machineName -Name $service 
			if ($serviceStatus.status -eq "Running"){ 
				Write-Host $machineName  $serviceStatus.name  $serviceStatus.status -ForegroundColor Green  
				$svcName = $serviceStatus.name  
				$svcState = $serviceStatus.status          
				Add-Content $report "<tr'>"  
				Add-Content $report "<td bgcolor= '#33ffff' align=center> $machineName</td>"  
				Add-Content $report "<td bgcolor= '#33ffff' align=center> $svcName</td>"  
				Add-Content $report "<td bgcolor= '#33ffff' align=center>$svcState</td>"  
				Add-Content $report "</tr>"        
			} 
			else{  
				Write-Host $machineName  $serviceStatus.name  $serviceStatus.status -ForegroundColor Red  
				$svcName = $serviceStatus.name  
				$svcState = $serviceStatus.status           
				Add-Content $report "<tr>"  
				Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$machineName</td>"  
				Add-Content $report "<td bgcolor= 'GainsBoro' align=center>$svcName</td>"  
				Add-Content $report "<td bgcolor= 'Red' align=center>$svcState</td>"  
				Add-Content $report "</tr>"    
			}  
		}  
	}  
}  
servicestatus $ServerList $ServicesList 

Add-content $report  "</table>"  
Add-Content $report "</body>"  
Add-Content $report "</html>"  

(2)效果图




参考资料:

[1]Sukhija Vikas.Monitor Windows Servicesstatus Remotely.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

午夜安全

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值