檢查郵箱數據庫狀態並生成報表腳本
註意:
1.使用前修改下裏面的一些參數(原作者都貼心的標註了)
2.有必要的話修改下腳本的執行策略
效果截圖:
計劃任務截圖:
腳本內容:
add-pssnapin microsoft.exchange* #加載EMS不然exchange命令無法識別
$Smtp="mail.demo.com"#需要修改的地方$AdminEamil="[email protected],[email protected]"#需要修改的地方
br/>$AdminEamil="[email protected],[email protected]"#需要修改的地方
$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 += "<tr style=""font-size:12px; font-family:‘微軟雅黑‘;$trbgcolor"" align=""center"">
<td height=""30"">$sem</td>
<td align=""left"">$errmess</td></tr>"
}
#判斷是否為主庫
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 += "<tr style=""font-size:12px; font-family:‘微軟雅黑‘;$trbgcolor"" align=""center"">
<td height=""30"">$mbs</td>
<td>$sdn</td>
<td bgcolor=""$bordercolor"">??$SStatus??</td>
<td bgcolor=""$bgindex"">??$SConIndexState??</td>
<td>??$scql??</td>
<td>??$srql??</td>
<td>??$sac??</td>
<td bgcolor=""$bordercolor"">??$sem??</td>
<td>??$seei??</td>
</tr>"
}
$Output = "<font size=""1"" face=""Arial,sans-serif"">
<h3 align=""center"">Exchange 數據庫運行狀態報表</h3>
<h5 align=""center"">生成時間 $((Get-Date).ToString())</h5>
</font>
<table width=""100%"" bordercolor=""#FFFFFF"" border=""0"" cellspacing=""2"" cellpadding=""0"">
<tr style=""background-color:#090; color:#FFF; font-size:14px; font-family:‘微軟雅黑‘;"" align=""center"">
<td height=""30"">服務器名稱</td>
<td>數據庫名稱</td>
<td>狀態</td>
<td>內容索引狀態</td>
<td>復制隊列長度</td>
<td>重播隊列長度</td>
<td>是否為主動庫</td>
<td>錯誤信息</td>
<td>錯誤事件ID</td>
</tr>
$OutDB
</table>
<br><br><h5 align=""center"">錯誤代碼及錯誤信息</h5><br>
<table width=""100%"" bordercolor=""#FFFFFF"" border=""0"" cellspacing=""2"" cellpadding=""0"">
<tr style=""background-color:#f90; color:#FFF; font-size:14px; font-family:‘微軟雅黑‘;"" align=""center"">
<td>錯誤代碼</td>
<td>錯誤內容</td>
</tr>
$errmes
</table>
"
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("[email protected]") #需要修改處
$mailmessage.Subject = “[Report] Exchange 數據庫運行狀態報表”
$mailmessage.IsBodyHtml = $true
$mailmessage.Body += "`n" + $body
$smtpclient.Send($mailmessage)
}
sendmail $Output
檢查郵箱數據庫狀態並生成報表腳本