Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Enable email Archiving powershell script

Posted on 2013-12-15
11
Medium Priority
?
2,636 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 664 total points
ID: 39722000
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 71

Accepted Solution

by:
Qlemo earned 1336 total points
ID: 39722204
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
ID: 39723251
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
Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 1336 total points
ID: 39723727
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
 
LVL 23

Author Comment

by:Malli Boppe
ID: 39725827
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 71

Expert Comment

by:Qlemo
ID: 39726084
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
ID: 39727943
Yeah your last changes and  I have changed the first few lines to make it work
0
 

Expert Comment

by:Member_2_7965358
ID: 41658633
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
ID: 41658709
Thanks for the advice... But, I've already found the answer.

Ed
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As much as Microsoft wants to kill off PST file support, just as they tried to do with public folders, there are still times when it is useful or downright necessary to export Exchange mailboxes to PST files. Thankfully, it is still possible to e…
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
To show how to create a transport rule 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 Mail Flow >> Rules tab.:  To cr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

885 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