A- A A+

Exchange 2013 - Quota's standaardiseren

Een klant wilde graag de toegewezen mail quota's standaardiseren op Exchange 2013 volgens bepaalde vaste limieten. Om dit te bewerkstelligen zijn er twee methodieken:

  • Documenteren wat de limieten zijn en 1e/2e lijns het laten uitvoeren;
  • Documenteren en een script beschikbaar stellen.

Om dit te bewerkstelligen heb ik het volgende script geïmplementeerd:

<#

.SYNOPSIS
   Sets Mailbox Quota on single mailbox or multiple mailboxes.

.DESCRIPTION
   Mailbox Quota on single mailbox or multiple mailboxes. Is able to run in report only mode.
   The script accepts the following parameters:
   -Server or -Mailboxes - required either one, but not both
   -LogFile filespec     - Logfile to create
   -LogOnly              - Runs in report-mode only
   -Brief                - Reports only changed objects to output
                           (Logfile contains all results)             

.EXAMPLE
    .\Set-MailboxQuotas.ps1 -server myserver -logfile test.log -Logonly -Brief
      Creates a logfile named test.log with all mailboxes on server myserver
      and only dumps quota changes to output. The script does not modify any
      object.

.EXAMPLE
    .\Set-MailboxQuotas.ps1 -mailboxes *sales* -logfile sales-set.log -Brief
      Processes all mailboxes named *sales*, dumps the information to sales-set.log
      displays only changed objects and changes the quota of the mailboxes.

.EXAMPLE
    .\Set-MailboxQuotas.ps1 -mailboxes exam* -logfile exam-set.log
      Processes all mailboxes named exam*, dumps the information to exam-set.log
      displays all objects and changes the quota of the mailboxes affected.

.PARAMETER Mailboxes
    Mailbox specification. Required if -Server is not used.

.PARAMETER Server
    Server specification. Required if -Mailboxes is not used.

.PARAMETER LogFile
    Required. Specifies the log-file.

.PARAMETER LogOnly
    Optional. Lets the script run in report mode only.

.PARAMETER Brief
    Optional. If specified the script only returns changed objects or objects
    which are to be changed.
#>

Param (
        # Mailbox(es)
        [Parameter(Mandatory=$false)]
        [string] $Mailboxes,
        # Server
        [Parameter(Mandatory=$false)]
        [string] $Server,
        #Logfile
        [Parameter(Mandatory=$true)]
        [string] $Logfile,
        # Logonly
        [Parameter(Mandatory=$false)]
        [switch] $Logonly,
        # Brief
        [Parameter(Mandatory=$false)]
        [switch] $Brief
)
$Default= "Default"
$Quotas = @{}

function AddQuota( $item, $limit, $default, $warning, $send, $receive)
{
    $Quotas[$item] = @{}
    $Quotas[$item]["limit"]= $limit
    $Quotas[$item]["default"] = $default
    $Quotas[$item]["warning"] = $warning
    $Quotas[$item]["send"]= $send
    $Quotas[$item]["receive"] = $receive
}
#        El Test  Def    Warn  Send  Rece
AddQuota 0 41472 $False 46080 51200 56320
AddQuota 1 36864 $False 41472 46080 50688
AddQuota 2 32256 $False 36864 40960 45056
AddQuota 3 27648 $False 32256 35840 39424
AddQuota 4 23040 $False 27648 30720 33792
AddQuota 5 18432 $False 23040 25600 28160
AddQuota 6 16128 $False 18432 20480 22528
AddQuota 7 13824 $False 16128 17920 19712
AddQuota 8 11520 $False 13824 15360 16896
AddQuota 9 9216 $False 11520 12800 14080
AddQuota 10 6912 $False 9216 10240 11264
AddQuota 11 4608 $False 6912 7680 8448
AddQuota 12 3686 $False 4608 5120 5632
AddQuota 13 2765 $False 3686 4096 4506
AddQuota 14 1843 $False 2765 3072 3379
AddQuota 15 922 $False 1843 2048 2253
AddQuota 16 675 $False 922 1024 1126
AddQuota 17 450 $False 675 750 825
AddQuota 18 -1 $True $Default $Default $Default

function SetQuota( $Log, $File, $Brief, $i, $Size, $UseDefault, $oldWarning, $oldSend, $oldReceive, $newWarning, $newSend, $newReceive )
{
    # Determine if Quota will be changed
    if ($oldReceive -eq $newReceive) {
        $Change = $False
    } else {
        $Change = $True
    }
    # Report to screen and initialization
    if ($Change -eq $True) {
        Write-Host $i -NoNewline
        Write-Host "," -NoNewline
        Write-Host $Size -NoNewline -ForegroundColor Red
        Write-Host "," -NoNewline
        Write-Host "Yes" -ForegroundColor Green -NoNewline
        Write-Host ",$oldwarning,$oldSend,$oldReceive," -NoNewline
        Write-Host "$newWarning" -ForegroundColor Green -NoNewline
        Write-Host "," -NoNewline
        Write-Host "$newSend" -ForegroundColor Yellow -NoNewline
        Write-Host "," -NoNewline
        Write-Host "$newReceive" -ForegroundColor Red
        if ($Log -eq $False) {
            try {
                if ($UseDefault -eq $True) {
                    $iw= "Unlimited"
                    $psq= "Unlimited"
                    $prq= "Unlimited"               
                } else {
                    $iw= "$newWarning"+"MB"
                    $psq= "$newSend"+"MB"
                    $prq= "$newReceive"+"MB"
                }
                Set-Mailbox -identity $i -UseDatabaseQuotaDefaults $UseDefault -IssueWarningQuota $iw -ProhibitSendQuota $psq  -ProhibitSendReceiveQuota $prq -confirm:$False -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
                # Write Entry to log
                try {
                    "$i,$Size,$Change,$oldWarning,$oldSend,$OldReceive,$newWarning,$newSend,$newReceive" | Out-File -FilePath $File -Append -ErrorAction SilentlyContinue
                }
                catch {
                    # Nothing we can do
                }
                finally {
                    # No after work
                }
            }
            catch {
                Write-Host "Error setting Quota for User $Identity." -ForegroundColor Red
                $newReceive= "Error"
                $newSend= "Error"
                $newWarning= "Error"
                # Write Entry to log
                try {
                    "$i,$Size,Error,$oldWarning,$oldSend,$OldReceive,$newWarning,$newSend,$newReceive" | Out-File -FilePath $File -Append -ErrorAction SilentlyContinue
                }
                catch {
                    # Nothing we can do
                }
                finally {
                    # No after work
                }
            }
            finally {
                # No Afterwork
            }
        }
    } else {
        if ($Brief -eq $False) {
            Write-Host $i -NoNewline
            Write-Host "," -NoNewline
            Write-Host $Size -NoNewline
            Write-Host "," -NoNewline
            Write-Host "No" -NoNewline
            Write-Host ",$oldwarning,$oldSend,$oldReceive," -NoNewline
            Write-Host "$NewWarning,$NewSend,$NewReceive" 
        }
        # Write Entry to log
        try {
            "$i,$Size,$Change,$oldWarning,$oldSend,$OldReceive,$newWarning,$newSend,$newReceive" | Out-File -FilePath $File -Append -ErrorAction SilentlyContinue
            }
        catch {
            # Nothing we can do
        }
        finally {
            # No after work
        }
    }
}

$error.Clear()
$Err = $False
if (!$Server -and !$MailBoxes) {
    Write-Host "Either -server or -Mailboxes must be specified." -ForegroundColor Red
    $error.Add("Set-MailboxQuotas missing parameter -server or -Mailboxes."
    $Err= $True
} else {
    if ($server -and $Mailboxes) {
        Write-Host "Cannot use -server and -Mailboxes parameter at same time." -ForegroundColor Red
        $error.Add("Set-MailboxQuotas Cannot use -server and -Mailboxes parameter at same time."
        $Err= $True
    }
}
if (!$Logonly) {
  $LogOnly = $false
}
if (!$Brief) {
  $Brief = $false
}
if ($Err -eq $False) {
    cls
    Write-Host " "
    Write-Host " "
    Write-Host " "
    Write-Host " "
    Write-Host " "
    Write-Host " "
    Write-Host " "
    Write-Host " "
    Write-Host " "
    Write-Host "****************************************************************" -BackgroundColor DarkBlue -ForegroundColor White
    Write-Host "*   Mailbox Quota Correction script by Wim-Bart van der Waals  *" -BackgroundColor DarkBlue -ForegroundColor White
    Write-Host "*                   Version 1.1 - 19th nov 2014                *" -BackgroundColor DarkBlue -ForegroundColor White
    Write-Host "****************************************************************" -BackgroundColor DarkBlue -ForegroundColor White
    Write-Host "Logging to: $LogFile" -ForegroundColor DarkYellow
    # Clean up log file if allready exists.
    try {
        Remove-Item $Logfile -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
        }
    catch {
        # Nothing to do
    }
    finally {
        # Write header to logfile
       "Mailbox,Size/MB,Change,CurrentWarning/MB,CurrentProhibitSend/MB,CurrentProhibitSendReceive/MB,NewWarning/MB,NewProhibitSend/MB,NewProhibitSendReceive/MB" | Tee -FilePath $Logfile
        $error.Clear()
    }
    Write-Host "Getting Mailboxes" -ForegroundColor Green
    # Get the Mailboxe(s)
    try {
        if (!$Mailboxes) {
            $Msg = "Invalid server or server not available."
            $mailboxen = Get-Mailbox -Server $Server -resultsize unlimited  -ErrorAction Stop
        } else {
            $Msg = "Invalid mailbox."
            $mailboxen = Get-Mailbox $Mailboxes -resultsize unlimited -ErrorAction Stop
        }
    }
    catch {
        # Set $Err to true so the next loop will fail.
        $Err= $True
        Write-Host "Script aborted with exception: $Msg" -ForegroundColor Red
        $Error.Add("Set-MailBoxQuotas.ps1 Script aborted with exception: $Msg")
    }
    finally {
        # Nothing to do
    }
    if ($Err -eq $False) {
        # Initialize loop and predump some information 
        $counter= 0
        $boxes= $mailboxen.count
        Write-Host "Found $boxes mailboxes." -ForegroundColor Green
        ForEach ($mailbox in $mailboxen) {
            $counter++
            Write-Progress -Activity "Processing mailboxes" -status "[$counter/$boxes] Process $mailbox" -percentComplete ($counter / $boxes*100) 
            # Get the information needed to process
            try {
                $stats = Get-MailboxStatistics $mailbox
                if ($mailbox.UseDatabaseQuotaDefaults) {
                    $a= $Default
                    $b= $Default
                    $c= $Default
                } else {
                    try {
                        $a= $mailbox.IssueWarningQuota.Value.ToMB()
                        $b= $mailbox.ProhibitSendQuota.Value.ToMB()
                        $c= $mailbox.ProhibitSendReceiveQuota.Value.ToMB() 
                    }
                    catch {
                        $a= "Unlimited"
                        $b= "Unlimited"
                        $c= "Unlimited"
                    }
                    finally {
                    }
                }
                $size= $stats.TotalItemSize.Value.ToMB()
                $i= $mailbox.Identity
                $count = 0
                while ($count -le $Quotas.Count-1) {
                    $Quota = $Quotas[$count]
                    $ql = $Quota.limit
                    $qd = $Quota.default
                    $qw = $Quota.warning 
                    $qs = $Quota.send
                    $qr = $Quota.receive
                    if ($size -gt $ql) {
                       SetQuota $LogOnly $Logfile $Brief $i $size $qd $a $b $c $qw $qs $qr
                       $count= $Quotas.Count #Force loop end
                    }
                    $count++
                }
            }
            catch {
                # Ignore
            }
            finally {
                # Ignore
            }
        }
    }
}