使用PowerShell脚本来轮训Exchange数据库的状态

这是来博客园的第一篇文章...当然...我在很多地方贴过这篇文章..如果你在其他地方看过,请不要介意。

常识性的,我会给出这篇文章“真正”的原创作者,博主的博客中有更多和Exchange以及PowerShell相关的内容,如果你感兴趣,将非常能吸引你。

http://msroger.blog.51cto.com/3571749/759322

 

Write-Host "开始之前,需要指定邮箱服务器位置,例如:exchangeserver.contoso.com" 

$exserverName = Read-Host; 

#发送远程指令,下载Exchange"管理程序集"

$psUri = "http://$exserverName/powershell"; 

$usecredential = Get-Credential 
$session=New-PSSession -ConfigurationName microsoft.exchange -ConnectionUri "$psUri" -Authentication kerberos -Credential $usecredential Import-PSSession $session Write-Host "请指定发送电子邮件的地址" $messageFrom=Read-Host; $mailCredential = Get-Credential; $from = New-Object System.Net.Mail.MailAddress $messageFrom Write-Host "请指定接收提示的电子邮件地址" $messageTo = Read-Host; Write-Host "开始之前,需要指定轮询频率,请输入秒数,例如:60" $queryTime = Read-Host; Write-Host "程序开始,每$queryTime 秒轮询一次"; $continueAlways = 1; while($continueAlways=1){ $date = Get-Date; Write-Host $date; $mailDatabases = Get-MailboxDatabase -Status foreach($db in $mailDatabases) { $dbname = $db.Name; $database = Get-MailboxDatabase –Identity $dbname -Status if($database.Mounted -eq $false){ Write-Host "$dbname 运转正常"; } else{ #提示警告 $alertMessage = "Alert!Database named "+$database.Identity.ToString()+" is not working!"; $to=New-Object System.Net.Mail.MailAddress "$messageTo"; #新建邮件 $message = new-object System.Net.Mail.MailMessage $from, $to; $message.Subject = "Alert From ExchangeServer Named $exserverName"; $message.Body = "$alertMessage" #设置发送邮件客户端 $server = "exsvr.search.ow" $client = new-object system.net.mail.smtpclient $server $client.Credentials = $mailCredential # 发送邮件 Write-Host "开始尝试发送警告邮件..." try { $client.Send($message) #保留这段代码,因为它的输出方式值得学习 #"Message to: {0}, from: {1} has beens successfully sent" -f $from, $to Write-Host "$alertMessage" "已经发送邮件至 $messageTo"; } catch { foreach($s in $Error){ "发生异常: {0}" -f $s.ToString() ; } } #这是先前使用powershell发送邮件的做法 #Send-MailMessage -From "[email protected]" -To "$messageTo" -Subject"Alert From ExchangeServer Named $exserverName" #-Body "$alertMessage" -SmtpServer "$exserverName"; #之所以这种方式开始的时候行不通,是因为没有提供用户凭据 #可以使用Get-Credential获取一个凭据,然后作为参数在Send-MailMessage指令中使用 } } Start-Sleep -Seconds $queryTime; }

 PowerShell和Exchange管理和C#和.NET放在一起将会是非常有趣的东西,我会整理更多的内容,所以暂时原谅我偷懒照搬吧。同时希望能在这里交到朋友,一起讨论,共同进步。

另外,本文章算不得原创,请随意引用。

你可能感兴趣的:(powershell)