Powershell检查邮件队列设置阈值,通过html形式进行邮件告警

为了完善公司的整体邮件质量,博主通过zabbix监控了exchange的所有微软推荐项目,并写了很多powershell来辅佐,

旨在更大程度上提高整体的邮件性能

 

这篇文章主要是讲通过powershell监控邮件队列,因为一些历史原因,公司有很多业务系统的退信,收件人失效,错误

邮件的退信会长时间停留在提交队列中,默认情况下,这些队列中的邮件会在48小时候被清除,队列中集攒过多邮件也

会一定程度上影响邮件整体性能,且作为一个有轻微强迫症的人,博主也十分希望列队中是十分清爽的

 

废话不多说,先说该目的的核心命令,其实就以一条十分简单的命令展开“Get-Message”,全部脚本如下


#导入 Powershell Exchange 模块
Add-PSSnapin microsoft.exchange*

#取值所有Exchange Server的名称,创建一个新的数组
$computername = (Get-TransportService).name
$sum = 0 #将失败队列总数归0
$array=@()
[System.Collections.ArrayList]$arraylist = $array

#定义字符类型
[String] $body = $null

#输出HTML报告
$report = "d:\QueueCheck\QueueCheck.html"
if((test-path $report) -like $false)
{
new-item $report -type file
}

#创建HTML
$msg = $report

Clear-Content $msg
Add-Content $msg "<html>"
Add-Content $msg "<body>"
Add-Content $msg "<table width='60%' border='1' cellspacing='0' align='center'>"
Add-Content $msg "<tr><td colspan='2' align='center'><font face='tahoma' color='#003399' size='4'><strong>Error Check</strong></font></td></tr>"
Add-Content $msg "<tr bgcolor='skyblue'>"
Add-Content $msg "<td width='60%' align='center'><B>Server Name</B></td>"
Add-Content $msg "<td width='40%' align='center'><B>Error Count</B></td>"
Add-Content $msg "</tr>"

#检查每一个Server的队列状态
foreach($name in $computername){
#HTML 另起一行
Add-Content $msg "<tr align='center'>"
Add-Content $msg "<td>$name</td>"
$count = (Get-Message -ResultSize Unlimited -Queue $name\submission | where {$_.subject -like "*Error*" -and $_.LastError -ne $null}).count
$null = $arraylist.Add($count)
$sum+=$count
 
if($count -gt 200){
Add-Content $msg "<td bgcolor= 'Red' align=center><B>$count</B></td>"
}
else{
Add-Content $msg "<td>$count</td>"
}

Add-Content $msg "</tr>"
}

$max = ($arraylist | Measure-Object -Maximum).Maximum

#关闭HTML
Add-Content $msg "</table>"
Add-Content $msg "</body>"
Add-Content $msg "</html>"

#定义邮件内容
$body = Get-Content $report

#发送邮件
if($max -gt 200 -or $sum -gt 1500){
Send-MailMessage -SmtpServer xxx -From xxx -To xxx -Cc xxx -Subject QueueCheck -BodyAsHtml $body
}

#判断ErrorQueue状态
if ($max -gt 200 -or $sum -gt 1500)
{
    $status = "Error"
}
else {
    $status = "OK"
}

#$message = "Total=$sum,Max=$max,Status=$status"

#连接MySQL
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$mySQLDataDLL = "C:\mysql\MySql.Data.dll"
[void][system.reflection.Assembly]::LoadFrom($mySQLDataDLL)

$Server = "xxx.xxx.xxx.xxx"
$Database = "xxx"
$user = "xxx"
$Password = "xxx"
$charset = "utf8"
$connectionString = "server=$Server;uid=$user;pwd=$Password;database=$Database;charset=$charset;SslMode=none"
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection($connectionString)
$connection.Open()

#执行MySQL
$insert = "INSERT INTO sys_detect (name,status,type) VALUES('ErrorQueue','$status','queue');"
$insertcommand = New-Object MySql.Data.MySqlClient.MySqlCommand
$insertcommand.Connection = $connection
$insertcommand.CommandText = $insert
$insertcommand.executenonquery()

#关闭连接
$connection.Close()
 
其实是一个很简单的powershell脚本,同时有制作html的用法,powersehll操作mysql的用法,powershell发送邮件的用法
 
效果图如下:

如果你希望进一步调整html的样式,可以通过http://www.w3school.com.cn/html/ 进行一定的学习,然后在线测试

转载于:https://www.cnblogs.com/zhr1217/p/9724390.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值