检查邮箱数据库状态并生成报表脚本
此脚本是群里面的哥们共享的,本人特整理出来分享给有需要的同学。
注意:
1.使用前修改下里面的一些参数(原作者都贴心的标注了)
2.有必要的话修改下脚本的执行策略
效果截图:
计划任务截图:
脚本内容:
add-pssnapin microsoft.exchange* #加载EMS不然exchange命令无法识别
$Smtp="mail.demo.com"#需要修改的地方$AdminEamil="zhangdl@demo.com,zhangsan@demo.com"#需要修改的地方
br/>$AdminEamil="zhangdl@demo.com,zhangsan@demo.com"#需要修改的地方
$i=0
$Searcher = Get-MailboxDatabase | Get-MailboxDatabaseCopyStatus
$bordercolor = '#eee'
$i = 0
foreach($Sear in $Searcher)
{
#$MailDB = Get-MailboxDatabase -Identity $Sear.Databasename
$sem = '无'
$bgindex = ''
$trbgcolor = ''
$errmess = ''
#判断库的复制和装入状态
if ($Sear.Status -eq 'Healthy')
{$SStatus = '复制正常'#状态,如已装载
$bordercolor = '#efefef'
}
Elseif($Sear.Status -eq 'Suspended')
{
$SStatus = '已搁置'
$bordercolor = '#FFFFCC'
}
Elseif($Sear.Status -eq 'Failed')
{
$SStatus = '失败'
$bordercolor = '#FF0000'
}
Elseif($Sear.Status -eq 'Mounted')
{
$SStatus = '已装载'
$bordercolor = '#ffffff'
}
Elseif($Sear.Status -eq 'Dismounted')
{
$SStatus = '已卸载'
$bordercolor = '#ffcc00'
}
Elseif($Sear.Status -eq 'ServiceDown')
{
$SStatus = '服务停止'
$bordercolor = '#FF0000'
}
else
{
$SStatus = $Sear.Status
}
#判断内容索引状态
if ($Sear.ContentIndexState -eq 'Healthy')
{
$SConIndexState = '正常'
}
elseif ($Sear.ContentIndexState -eq 'Failed')
{
$SConIndexState = '失败'
$bgindex = '#ff0000'
}
else
{
$SConIndexState = $Sear.ContentIndexState
$bgindex = '#ff0000'
}
#判断是否有错误
if ($Sear.ErrorMessage.length -gt 0)
{
$i = $i+1
$sem = '错误代码00'+$i
$errmess = $Sear.ErrorMessage
$errmes += "
}
#判断是否为主库
if ($Sear.ActiveCopy -eq $true)
{
$sac = '是'
}
else
{
$sac = '否'
$trbgcolor = 'background-color:#efefef'
}
$mbs = $Sear.MailboxServer
$sdn = $Sear.DatabaseName
$scql = $Sear.CopyQueueLength
$srql = $Sear.ReplayQueueLength
$seei = $Sear.ErrorEventId $OutDB += "$mbs $sdn $SStatus $SConIndexState $scql $srql $sac $sem $seei "
}
$Output = "
Exchange 数据库运行状态报表
生成时间 $((Get-Date).ToString())
服务器名称
数据库名称
状态
内容索引状态
复制队列长度
重播队列长度
是否为主动库
错误信息
错误事件ID
$OutDB
错误代码及错误信息
错误代码
错误内容
$errmes
"function sendmail($body)
{
$today = Get-Date
$SmtpClient = new-object system.net.mail.smtpClient
$mailmessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = $Smtp$mailmessage.from = $EmailSender$mailmessage.To.add($AdminEamil)$mailmessage.Priority=[system.net.mail.MailPriority]::High $mailmessage.Bcc.add("zhangdl@qunar.com") #需要修改处$mailmessage.Subject = "[Report] Exchange 数据库运行状态报表" $mailmessage.IsBodyHtml = $true$mailmessage.Body += "`n" + $body$smtpclient.Send($mailmessage)
}
sendmail $Output