Exchange ServerPowerShell

Exchange Server Mailbox Quota Notification

Bu makalede, belirli bir veritabanındaki kullanıcıların posta kutusu kotalarını denetlemek ve belirlenen eşik değeri aşan hesaplar için bildirim göndermek üzere hazırladığım PowerShell scriptini ele alacağız.

Ayrıca, script SMTP kimlik bilgilerini güvenli bir şekilde depolamak ve kullanmak için XML dosyası kullanmaktadır.

image 18
Exchange Server Mailbox Quota Notification 3

Exchange Server ortamlarında posta kutusu kotaları, sistem kaynaklarının verimli kullanımı için önem arz etmektedir. Kullanıcılar belirlenen kota limitlerini aştığında, sistem yöneticilerinin bu durumdan haberdar olması gerekmektedir.

Exchange Server Mailbox Quota Notification Script Detayları

Script, belirtilen Exchange veritabanındaki tüm kullanıcı posta kutularının kota kullanımını kontrol eder. Eğer kullanıcı kotası, belirlenen eşik değerin üzerindeyse, ilgili hesap bilgileriyle birlikte bir uyarı e-postası gönderilir.

Exchange Server Mailbox Quota Notification Script Yapılandırılması

  • Veritabanı ve Threshold Değeri Belirleme:
    • $Database = "DB01": Kontrol edilecek Exchange veritabanı.
    • $Threshold = "5": Yüzde olarak belirlenen kota kullanım eşik değeri.
  • SMTP Sunucu Ayarları:
    • $From = "[email protected]": Gönderici e-posta adresi.
    • $Server = "mail.cengizyilmaz.net": Kullanılacak mail sunucusu.
    • $Port = 587: Mail sunucusu port numarası.
    • $To = "[email protected]": Bildirim e-postasının gönderileceği adres.
    • $Subject = "Journal Mailbox Kota Uyarısı!": E-posta konusu.
  • SMTP Kimlik Bilgileri Yönetimi:
    • Kullanıcıdan SMTP kimlik bilgileri alınır ve güvenli bir şekilde $CredObj dosyasına kaydedilir.
    • Kimlik bilgileri, ilerleyen kullanımlar için XML formatında saklanır ve gerektiğinde yüklenir.
  • Kota Durum Kontrolü ve E-Posta İletimi:
    • Script, veritabanındaki tüm kullanıcıların posta kutusu istatistiklerini alır.
    • Kota kullanımı belirlenen eşik değeri aşan kullanıcılar için HTML formatında bir rapor oluşturulur ve e-posta yoluyla gönderilir.
  • Loglama ve Hatalar:
    • Tüm işlemler sırasında karşılaşılan hatalar $DirPathLog.txt dosyasına kaydedilir.
<#	
#################################################################################################################
# Yayınlanma Tarihi: 17.02.2023
# Cengiz YILMAZ
# MCT
# https://cozumpark.com/author/cengizyilmaz
# https://cengizyilmaz.com.tr
# [email protected]
#
##################################################################################################################
#>
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn
 
# Set the database, threshold, from, server, port, to, subject and body variables
$Database = "DB01"
$DirPath = "C:Quota"
$Threshold = "5"
$From = "[email protected]"
$Server = "mail.cengizyilmaz.net
$Port = 587
$To = "[email protected]"
$Subject = "Journal Mailbox Kota Uyarısı!"
$Body = "<html><body><h4>$database içerisinde bulunan hesapların kota durumu %$threshold değerinden fazladır.</h4><table border='3'><tr><th>Display Name</th><th>Email Address</th><th>Quota Usage</th></tr>"


$DirPathCheck = Test-Path -Path $DirPath
If (!($DirPathCheck))
{
        Try
        {
                 
                 New-Item -ItemType Directory $DirPath -Force
        }
        Catch
        {
                 $_ | Out-File ($DirPath + "" + "Log.txt") -Append
        }
}
#CredObj path
$CredObj = ($DirPath + "" + "Quota.cred")
#Check if CredObj is present
$CredObjCheck = Test-Path -Path $CredObj
If (!($CredObjCheck))
{
        "$Date - INFO: creating cred object" | Out-File ($DirPath + "" + "Log.txt") -Append
        #SMTP Info
        $Credential = Get-Credential -Message "Please enter your Mail Server credential that you will use to send e-mail $fromAddress. If you are not using the account $fromAddress make sure this account has 'Send As' rights on $FromEmail."
        #Export cred obj
        $Credential | Export-CliXml -Path $CredObj
}
 
Write-Host "Importing Cred object..." -ForegroundColor Yellow
$Cred = (Import-CliXml -Path $CredObj)

 
# Get all mailbox users in the specified database
$mailboxUsers = Get-Mailbox -Database $Database -ResultSize unlimited | Where-Object {$_.RecipientTypeDetails -eq 'UserMailbox'}
 
# Loop through each mailbox user
foreach ($user in $mailboxUsers) {
    # Get the mailbox statistics
    $mailbox = Get-MailboxStatistics -Identity $user.DistinguishedName
 
    # Calculate the quota usage percentage
    $quotaUsage = ($mailbox.TotalItemSize.Value.ToMB() / $user.ProhibitSendQuota.Value.ToMB()) * 100
 
    # Check if the quota usage exceeds the threshold
    if ($quotaUsage -ge $Threshold) {
        # Create the HTML table row with the user's display name, email address, and quota usage in red text
        $Body += "<tr><td>$($user.DisplayName)</td><td>$($user.PrimarySmtpAddress)</td><td><font color='red'>$($quotaUsage.ToString("0.00"))%</font></td></tr>"
 
# Create mail message object
$message = New-Object System.Net.Mail.MailMessage
$message.From = $From
$message.To.Add($To)
$message.Subject = $subject
$message.Body = $body
$message.Priority = "High"
$message.IsBodyHtml = $true
    }
}
 
# Close HTML table and body
$Body += "</table></body></html>"
 

 
# Create the SMTP client object
$client = New-Object System.Net.Mail.SmtpClient
$client.Host = $Server
$client.Port = $Port
$client.Credentials = $Cred

 
# Send the email
$client.Send($message)
 
# Dispose of the message object
$message.Dispose()

İlgili Makaleler

Bir yanıt yazın

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Başa dön tuşu