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.


猜你喜欢

转载自blog.csdn.net/southwind0/article/details/80459996