#导入 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 ""
Add-Content $msg "
"Add-Content $msg "
Add-Content $msg "
Error Check"Add-Content $msg "
"Add-Content $msg "
Server Name"Add-Content $msg "
Error Count"Add-Content $msg "
"#检查每一个Server的队列状态
foreach($name in $computername){
#HTML 另起一行
Add-Content $msg "
"Add-Content $msg "
$name"$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 "
$count"}
else{
Add-Content $msg "
$count"}
Add-Content $msg "
"}
$max = ($arraylist | Measure-Object -Maximum).Maximum
#关闭HTML
Add-Content $msg "
"Add-Content $msg ""
Add-Content $msg ""
#定义邮件内容
$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/ 进行一定的学习,然后在线测试