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)






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章