Link to home
Start Free TrialLog in
Avatar of Carl Billington
Carl BillingtonFlag for Australia

asked on

Apply mailbox quotas based on current mailbox size (Powershell)?

RE: Exchange 2010
 
I have recently joined a company that have completely unrestructed mailbox quotas and I'm trying to decide the most appropriate way of introducting / enforcing mailbox quotas for the organisation.
 
Therefore I would like to generate a powershell script that will assign specific mailbox quotas based on the size of the individuals mailbox.
 
For example;
 
If a user has a mailbox size between 1 > 2 GB I would like the script to apply the following quota; -IssueWarningQuota 950MB -ProhibitSendQuota 2048MB -ProhibitSendReceiveQuota 2048MB
 
If a user has a mailbox size between 2 > 3 GB I would like the script to apply the following quota; -IssueWarningQuota 1024MB -ProhibitSendQuota 3072MB -ProhibitSendReceiveQuota 3072MB
 
If a user has a mailbox size between 3 > 4 GB I would like the script to apply the following quota; -IssueWarningQuota 2048MB -ProhibitSendQuota 4096MB -ProhibitSendReceiveQuota 4096MB
 
Can you help with this powershell script? I would also like to assign exclusions if possible?
Avatar of Rajitha Chimmani
Rajitha Chimmani
Flag of United States of America image

You can use the below script. You may conditions to assign exclusions.

$AllMailboxes = Get-Mailbox -resultsize unlimited | Get-MailboxStatistics
foreach($Mailbox in $AllMailboxes){
$SizeMB = $Mailbox.TotalItemSize.Value.ToMB()
if(($SizeMB -lt 2048) -or ($SizeMB -gt 1024)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 950MB -ProhibitSendQuota 2048MB -ProhibitSendReceiveQuota 2048MB}
if(($SizeMB -lt 3072) -or ($SizeMB -gt 2048)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 1024MB -ProhibitSendQuota 3072MB -ProhibitSendReceiveQuota 3072MB}
if(($SizeMB -lt 4096) -or ($SizeMB -gt 3072)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 2048MB -ProhibitSendQuota 4096MB -ProhibitSendReceiveQuota 4096MB}
}
Avatar of Carl Billington

ASKER

Wow, I'm going to test this shortly. How do I assign exclusions? Say for example I wanted to exclude; carl, anna, robert
Ok. In that case add one more condition like

if(($Mailbox.Displayname -like *carl*) -or ($Mailbox.Displayname -like *anna*) -or ($Mailbox.DisplayName -like *robert*)){Set-Mailbox $Mailbox.DisplayName -IssueWarningQuota unlimited -ProhibitSendQuota unlimited -ProhibitSendReceiveQuota unlimited -UseDatabaseRetentionDefaults $false }

Or if you are looking to exclude those mailboxes from changing the limits then you can add the below condition to first command.

$AllMailboxes = Get-Mailbox -resultsize unlimited | Get-MailboxStatistics | where {($_.Displayname -like *cart*) -or ($_.Displayname -like *anna*) -or ($_.Displayname -like *robert*)}

So, this will omit this mailboxes from the initial list itself. If you have the exact display name then you can modify the condition as $_.DisplayName -eq 'DisplayName
So to confirm this is the command below? (this will run the command for all mailboxes excluding anna, robert and carl)

Let me know?

$AllMailboxes = Get-Mailbox -resultsize unlimited | Get-MailboxStatistics | where {($_.Displayname -like *cart*) -or ($_.Displayname -like *anna*) -or ($_.Displayname -like *robert*)}

$AllMailboxes = Get-Mailbox -resultsize unlimited | Get-MailboxStatistics
foreach($Mailbox in $AllMailboxes){
$SizeMB = $Mailbox.TotalItemSize.Value.ToMB()
if(($SizeMB -lt 2048) -or ($SizeMB -gt 1024)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 950MB -ProhibitSendQuota 2048MB -ProhibitSendReceiveQuota 2048MB}
if(($SizeMB -lt 3072) -or ($SizeMB -gt 2048)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 1024MB -ProhibitSendQuota 3072MB -ProhibitSendReceiveQuota 3072MB}
if(($SizeMB -lt 4096) -or ($SizeMB -gt 3072)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 2048MB -ProhibitSendQuota 4096MB -ProhibitSendReceiveQuota 4096MB}
}
Exactly. You got me right.
I get this error?

You must provide a value expression on the right-hand side of the '-like' operator.
At C:\powershell.ps1:1 char:104
+ AllMailboxes = Get-Mailbox -resultsize unlimited | Get-MailboxStatistics | where {($_.Displayname -like <<<<  *cart*)
 -or ($_.Displayname -like *anna*) -or ($_.Displayname -like *robert*)}
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : ExpectedValueExpression

[PS] C:\>
Ohh...i missed the quotes.

it must be like "*cart*"
Please note that this condition will skip all the mailboxes which has the words "cart" or "anna" or "robert" in any part of the displayname.
:( see error below;

[PS] C:\>.\powershell.ps1
The term 'AllMailboxes' is not recognized as the name of a cmdlet, function, script file, or operable program. Check th
e spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\powershell.ps1:1 char:13
+ AllMailboxes <<<<  = Get-Mailbox -resultsize unlimited | Get-MailboxStatistics | where {($_.Displayname -like "carl")
 -or ($_.Displayname -like "anna") -or ($_.Displayname -like "robert")}
    + CategoryInfo          : ObjectNotFound: (AllMailboxes:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

[PS] C:\>
You missed $ before AllMailboxes. Also, you removed asterisk (*). If you remove this you may not get correct results.

The first line must be

$AllMailboxes  = Get-Mailbox -resultsize unlimited | Get-MailboxStatistics | where {($_.Displayname -like "*carl*")
 -or ($_.Displayname -like "*anna*") -or ($_.Displayname -like "*robert*")}
The command is running without errors however the mailbox quotas are not working correctly. All mailboxes are configured with the same quota; -IssueWarningQuota 2048MB -ProhibitSendQuota 4096MB -ProhibitSendReceiveQuota 4096MB
 
$AllMailboxes = Get-Mailbox -resultsize unlimited | Get-MailboxStatistics | where {($_.Displayname -like "*carl*") -or ($_.Displayname -like "*anna*") -or ($_.Displayname -like "*robert*")}
$AllMailboxes = Get-Mailbox -resultsize unlimited | Get-MailboxStatistics
foreach($Mailbox in $AllMailboxes){
$SizeMB = $Mailbox.TotalItemSize.Value.ToMB()
if(($SizeMB -lt 512) -or ($SizeMB -gt 256)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 256MB -ProhibitSendQuota 512MB -ProhibitSendReceiveQuota 1024MB}
if(($SizeMB -lt 1048) -or ($SizeMB -gt 512)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 512MB -ProhibitSendQuota 1024MB -ProhibitSendReceiveQuota 2048MB}
if(($SizeMB -lt 2048) -or ($SizeMB -gt 1024)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 950MB -ProhibitSendQuota 2048MB -ProhibitSendReceiveQuota 2048MB}
if(($SizeMB -lt 3072) -or ($SizeMB -gt 2048)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 1024MB -ProhibitSendQuota 3072MB -ProhibitSendReceiveQuota 3072MB}
if(($SizeMB -lt 4096) -or ($SizeMB -gt 3072)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 2048MB -ProhibitSendQuota 4096MB -ProhibitSendReceiveQuota 4096MB}
} 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rajitha Chimmani
Rajitha Chimmani
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Any idea what were the limits for those Cara, Anna and Robert. They must be updated to old values as they were not skipped in your script.
The script is running but is not updating anyone's mailbox quotas ?
let me try again
It is applying correctly but "use mailbox database defaults" is still enabled for the users.
Ok..The parameter i gave was for Exchange 2007.

Instead of UseDatabaseRetentionDefaults $false, please set it to UseDatabaseQuotaDefaults $false
You have been amazing. Thank you so much. Truly an expert
Prefect. My pleasure :) Nice learning