Solved

Enable email Archiving powershell script

Posted on 2013-12-15
11
2,449 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 53

Assisted Solution

by:Will Szymkowski
Will Szymkowski earned 166 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 70

Accepted Solution

by:
Qlemo earned 334 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 334 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 70

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

Does Powershell have you tied up in knots?

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
This video discusses moving either the default database or any database to a new volume.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Suggested Courses

627 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