[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-08-23
20
Medium Priority
?
1,259 Views
Last Modified: 2012-12-02
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?
0
Comment
Question by:the_omnific
  • 10
  • 10
20 Comments
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 38324177
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}
}
0
 
LVL 1

Author Comment

by:the_omnific
ID: 38324187
Wow, I'm going to test this shortly. How do I assign exclusions? Say for example I wanted to exclude; carl, anna, robert
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 38324215
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
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
LVL 1

Author Comment

by:the_omnific
ID: 38324234
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}
}
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 38324237
Exactly. You got me right.
0
 
LVL 1

Author Comment

by:the_omnific
ID: 38324276
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:\>
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 38324297
Ohh...i missed the quotes.

it must be like "*cart*"
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 38324299
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.
0
 
LVL 1

Author Comment

by:the_omnific
ID: 38324311
:( 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:\>
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 38324322
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*")}
0
 
LVL 1

Author Comment

by:the_omnific
ID: 38324400
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

0
 
LVL 16

Accepted Solution

by:
Rajitha Chimmani earned 2000 total points
ID: 38324413
Hey...you forgot to remove the second line...So, even if you added conditions for Cara, Anna or Robert it would not have worked.

Please try below, it should help

$AllMailboxes = Get-Mailbox -resultsize unlimited | Get-MailboxStatistics | where {($_.Displayname -like "*carl*") -or ($_.Displayname -like "*anna*") -or ($_.Displayname -like "*robert*")}
foreach($Mailbox in $AllMailboxes){
$SizeMB = $Mailbox.TotalItemSize.Value.ToMB()
if(($SizeMB -lt 512) -and ($SizeMB -gt 256)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 256MB -ProhibitSendQuota 512MB -ProhibitSendReceiveQuota 1024MB}
if(($SizeMB -lt 1048) -and ($SizeMB -gt 512)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 512MB -ProhibitSendQuota 1024MB -ProhibitSendReceiveQuota 2048MB}
if(($SizeMB -lt 2048) -and ($SizeMB -gt 1024)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 950MB -ProhibitSendQuota 2048MB -ProhibitSendReceiveQuota 2048MB}
if(($SizeMB -lt 3072) -and ($SizeMB -gt 2048)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 1024MB -ProhibitSendQuota 3072MB -ProhibitSendReceiveQuota 3072MB}
if(($SizeMB -lt 4096) -and ($SizeMB -gt 3072)){Set-Mailbox $Mailbox.DisplayName -UseDatabaseRetentionDefaults $false -IssueWarningQuota 2048MB -ProhibitSendQuota 4096MB -ProhibitSendReceiveQuota 4096MB}
}  

If this works, we can check for all other mailboxes which got modified with old script.
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 38324424
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.
0
 
LVL 1

Author Comment

by:the_omnific
ID: 38324435
The script is running but is not updating anyone's mailbox quotas ?
0
 
LVL 1

Author Comment

by:the_omnific
ID: 38324439
let me try again
0
 
LVL 1

Author Comment

by:the_omnific
ID: 38324493
It is applying correctly but "use mailbox database defaults" is still enabled for the users.
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 38324520
Ok..The parameter i gave was for Exchange 2007.

Instead of UseDatabaseRetentionDefaults $false, please set it to UseDatabaseQuotaDefaults $false
0
 
LVL 1

Author Comment

by:the_omnific
ID: 38324544
You have been amazing. Thank you so much. Truly an expert
0
 
LVL 16

Expert Comment

by:Rajitha Chimmani
ID: 38324546
Prefect. My pleasure :) Nice learning
0
 
LVL 1

Author Comment

by:the_omnific
ID: 38653030
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
This article will help to fix the below errors for MS Exchange Server 2016 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
Screencast - Getting to Know the Pipeline
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question