1. 程式人生 > >檢查郵箱資料庫狀態並生成報表指令碼

檢查郵箱資料庫狀態並生成報表指令碼

此指令碼是群裡面的哥們共享的,本人特整理出來分享給有需要的同學。

注意:
1.使用前修改下里面的一些引數(原作者都貼心的標註了)
2.有必要的話修改下指令碼的執行策略

效果截圖:
檢查郵箱資料庫狀態並生成報表指令碼

計劃任務截圖:
檢查郵箱資料庫狀態並生成報表指令碼

指令碼內容:

add-pssnapin microsoft.exchange* #載入EMS不然exchange命令無法識別

$Smtp="mail.demo.com"#需要修改的地方br/>$AdminEamil="[email protected],[email protected]"#需要修改的地方
$EmailSender="[email protected]

"#需要修改的地方
$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 += "<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