Windows服务器性能监控脚本(powershell)

Powershell写的一个脚本,主要监控Windows服务器的一些参数,实现邮件输出巡检日报。

也可以添加IF条件语句,判断服务状态不是Running然后邮件报警(任务计划启动该脚本)。


巡检日报效果图如下:


具体代码如下:

#文件夹位置
$Folder="D:\Auto" 
#邮件内容格式为html文件,这里是创建文件的位置							
$Report="D:\Auto\ServiceReport.htm"	
#正则表达用于匹配IP				
$regex = [regex]"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"
#创建上述文件		
New-Item -ItemType File -Path $Folder -Name ServiceReport.htm -force	
Clear-Content $Report

add-content $report "<html>"
add-content $report "<head>"
add-content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
add-content $report '<title>Service Monitor</title>'
add-content $report '<STYLE TYPE="text/css">'
add-content $report "<!--"
add-content $report "td {"
add-content $report "font-family:Microsoft YaHei;"
add-content $report "font-size: 13px;"
add-content $report "border-top: 1px solid #000000;"
add-content $report "border-right: 0px solid #000000;"
add-content $report "border-bottom: 1px solid #000000;"
add-content $report "border-left: 1px solid #000000;"
add-content $report "padding-top: 0px;"
add-content $report "padding-right: 0px;"
add-content $report "padding-bottom: 0px;"
add-content $report "padding-left: 2px;"
add-content $report "}"
add-content $report "body {"
add-content $report "margin-left: 5px;"
add-content $report "margin-top: 5px;"
add-content $report "margin-right: 0px;"
add-content $report "margin-bottom: 10px;"
add-content $report ""
add-content $report "table {"
add-content $report "border: thin solid #FF0000;" 
add-content $report "}"
add-content $report "-->"
add-content $report "</style>"
add-content $report "</head>"
add-content $report "<body>"
add-content $report "<table width='1000' border='0px' cellspacing='0' cellpadding:3>"
add-content $report "<td colspan='5' height='25' align='center'>"
add-content $report "<font size='4'><strong>服务器巡检</strong></font>"
add-content $report "</td>"
add-content $report "</tr>"
add-content $report "<tr>"
add-content $report "<td width='20%'><strong>巡检时间:</strong></td>"
add-content $report "<td width='80%' colspan='4'>$(Get-Date)</td>"
add-content $report "</tr>"
add-content $report "<font color='#FFFFFF'><tr bgcolor='#36648B'><strong>"
add-content $report "<td width='200'>服务器名称</td>"
add-content $report "<td width='200'> 服务器IP</td>"
add-content $report "<td width='200'> 应用描述</td>"
add-content $report "<td width='200'> 关键服务</td>"
add-content $report "<td width='200' > 系统盘剩余空间</td>"
add-content $report "</strong></tr></font>"

List="XXXX","XXXX","XXXX"

Foreach ($MachineName in $List)
{ 
 #获取服务信息(XXXX为服务的显示名称)
 $Service=Get-WmiObject Win32_Service -ComputerName $MachineName | where {$_.DisplayName -match 'XXXX'}	
 #获取原始IP信息
 $ip=Get-WmiObject win32_networkadapterconfiguration -ComputerName $MachineName |Out-String 	
 #获取C盘剩余空间				
 $disk=[double](“{0:N1}”-f $((Get-WMIObject Win32_LogicalDisk -ComputerName $MachineName |where {$_.DeviceID –match “C”}).FreeSpace/1024MB))	
 #获取CPU利用率(运行效率过低,未启动)		
 #$cpu=“{0:N1}”-f [Double]$((Get-Counter -ComputerName $MachineName -Counter "Processor(_Total)\% Processor Time"|out-String).Split(":")[-1]).Trim(" .-`t`n`r")
 #获取可用物理内存(运行效率过低,未启动)	
 #$Memory=“{0:N1}”-f ([Double]$((Get-Counter -ComputerName $MachineName -Counter "\Memory\Available Bytes"|out-String).Split(":")[-1]).Trim(" .-`t`n`r")/1GB)		
 #获取服务状态
 $ServiceState = $Service.State		
 #正则匹配服务器IP
 if($ip -match $regex){			
 $ip2=$Matches.Values			
 add-content $report "<tr>"
 #服务器名称
 add-content $report "<td>$MachineName</td>"
 #服务器IP	
 add-content $report "<td>$ip2</td>"	
 #应用描述
 add-content $report "<td>XXXX</td>"	
 #关键服务
 if ($ServiceState -match "stopped") {add-content $report "<td bgcolor=red>$ServiceState</td>"} else {add-content $report "<td bgcolor=#FFFFFF >$ServiceState</td>"}	
 #系统盘剩余空间
 if ($disk -le 2)  {add-content $report "<td bgcolor=red>$disk GB</td>"} else {add-content $report "<td bgcolor=#FFFFFF >$disk GB</td>"}				
 add-content $report "</tr>"
}} 

add-content $report "</table>"
add-content $report "</body>"
add-content $report "</table>"
add-content $report "</html>"

#邮件中转服务器地址
$smtphost = "xxxx" 
#发件人邮件地址	
$from ="xxxx" 		
#收件人邮件地址
$to ="xxxx" 		
#邮件标题
$subject = "xxxx" 	
$body = Get-Content $report
$smtp= New-Object System.Net.Mail.SmtpClient $smtphost
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.isBodyhtml = $true
$smtp.send($msg)







猜你喜欢

转载自blog.csdn.net/u013181216/article/details/70805654
今日推荐