Solved

Enable email Archiving powershell script

Posted on 2013-12-15
11
2,084 Views
Last Modified: 2016-06-17
I have a list users  with alias name in  a text file. I want a script which can check which user database the user mailbox is on and depending on that create a archive mailbox for that user. Also apply a retension policy for that user.

Example: User1 is  either on maildatabase1 or maildatabase2 need to enable archiving on archivedatabase1 and need to enable retention policy

maildatabase3 or maildatabase4 ->archivedatabase2 and so on
0
Comment
Question by:Malli Boppe
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 53

Assisted Solution

by:Will Szymkowski
Will Szymkowski earned 166 total points
Comment Utility
I have created a script for accomplishing the above question. See below script for details...

The script below has variables that need to match up with your database names and archive database names below.

Import-Module activedirectory
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin
$MBXDB1 = Write-Host "MailboxDataBase1"
$MBXDB2 - Write-Host "MailboxDatabase2"
$Archive1 = Write-Host "MailboxArchive1"
$Archive2 = Write-Host "MailboxArchive2"
$UsersList = Get-Content "c:\yourFILEhere.txt"
foreach ($User in $UserList) {
$User.Alias
Get-MailboxStatistics -Identity $User.Alias
    if ($_.Datbase -eq $MBXDB1) {
        Get-Mailbox -Name $User.Alias | New-Mailbox -Archive -ArchiveDatabase $Archive1
    } or {
    if ($_.Database -eq $MBXDB2) {
        Get-Mailbox -Name $User.Alias | New-Mailbox -Archive -ArchiveDatabase $Archive2
        }
    }
}

Open in new window

Will.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 334 total points
Comment Utility
Spec01,

Get-Mailbox should provide you with the Database value already, so no need to use Get-MailBoxStatistics, And the AD module isn't used here, so no need to import that.
Also, setting the vars doesn't make sense as you do here, Write-Host doesn't return anything.
And the IF syntax is wrong, too.

My (untested) suggestion is:
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin

Get-Content "C:\Users.csv" | Get-Mailbox | % {
  if ("maildatabase1", "maildatabase2" -contains $_.Database)
  { $arcDb = "archivedatabase1" }
  elseif ("maildatabase3", "maildatabase4" -contains $_.Database)
  { $arcDb = "archivedatabase2" }
  # and so on
  $_ | New-Mailbox -Archive -ArchiveDatabase $arcDb
}

Open in new window

This could be done somewhat more elegant with a switch. You also might have to supply the -RetensionPolicy parameter when creating the archive mailbox to enable it, but what to provide with that is determined by your Exchange settings.
0
 
LVL 23

Author Comment

by:Malli Boppe
Comment Utility
Thanks Guys, the below script  works for a single user but when the CSV file has multiple users throws up an error "Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently"

Get-Content "C:\temp\Users.csv" | Get-Mailbox | % {
  if ("mailbox database1", "mailbox database2" -contains $_.Database)
  { $arcDb = "archive database1" }
  elseif ("mailbox database3", "mailbox database4" -contains $_.Database)
  { $arcDb = "archive database2" }
elseif ("mailbox database5", "mailbox database6" -contains $_.Database)
  { $arcDb = "archive database3" }

  $_ | enable-Mailbox -Archive -ArchiveDatabase $arcDb
 $_ | Set-mailbox -RetentionPolicy "90 Day Archive - All Mail Folders (Never Delete)"
}

Open in new window

0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 334 total points
Comment Utility
Try if this helps:
  Enable-Mailbox -Identity $_.Identity -Archive -ArchiveDatabase $arcDb
  Set-mailbox -Identity $_.Identity -RetentionPolicy "90 Day Archive - All Mail Folders (Never Delete)"

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 23

Author Comment

by:Malli Boppe
Comment Utility
This is what I had to do
$list=Get-Content "C:\temp\Users.csv" | Get-Mailbox

$list  | % {
  if ("mailbox database1", "mailbox database2" -contains $_.Database)
  { $arcDb = "archive database1" }
  elseif ("mailbox database3", "mailbox database4" -contains $_.Database)
  { $arcDb = "archive database2" }
elseif ("mailbox database5", "mailbox database6" -contains $_.Database)
  { $arcDb = "archive database3" }


Enable-Mailbox -Identity $_.Identity -Archive -ArchiveDatabase $arcDb
Set-mailbox -Identity $_.Identity -RetentionPolicy "90 Day Archive - All Mail Folders (Never Delete)"
 }

Open in new window

1
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
So my last change works, or do you really need to have fetched all mailboxes before going into the processing pipeline?
0
 
LVL 23

Author Comment

by:Malli Boppe
Comment Utility
Yeah your last changes and  I have changed the first few lines to make it work
0
 

Expert Comment

by:Member_2_7965358
Comment Utility
In addition to this script (Malli's last one) worked really well... I'd like to add a couple of pieces to the puzzle.

Is there an easy way to add the Quota Limit and Quota Warning?

Again, great script!

Ed Resleff
0
 

Expert Comment

by:Member_2_7965358
Comment Utility
Thanks for the advice... But, I've already found the answer.

Ed
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
This article will help you understand what HashTables are and how to use them in PowerShell.
In this video we show how to create a Contact in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Contact ta…
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now