Enable-PersonalArchive.ps1 v1.2

#MSExchange

During migrations to Exchange 2010,  we are also using a tool call Transvault to export data from Enterprise Vault in to Exchange.  As you would expect this is a massive task.  Originally we moved the data in to the primary mailbox, but with laptop rebuilds and slow links while syncing the data we switched Transvault to move the data in to an Exchange Personal Archive, and it works like a dream.

This results in me having to enable the personal archive for different users and as expected I scripted it and here it is.  The help text at the top should explain everything you need to know .. Enjoy

<#
.NOTES
  NAME: Enable-PersonalArchive.ps1
  AUTHOR: Paul Flaherty
  Last Edit: v1.2 [4 April 2012]
  v1.0 21 Feb 2012 : A script is born
  v1.1 02 Mar 2012 : Added switches to allow only applying of policies
  v1.2 04 Apr 2012 : Added mailbox switch
.LINK
  blogs.flaphead.com 
.SYNOPSIS
  Enable Exchange 2010 Personal Archive from a CSV file
.DESCRIPTION
  This Script gets reads a CSV file that contains email addresses and enabled
  the Exchange 2010 Personal Archive.
.PARAMETER CSVIN
  Location of a csv file that contains a single column for email
  Default file is C:MigrationListArchivelist.csv
.PARAMETER RetentionPolicy
  Name of the Retention Policy to apply
.PARAMETER CreateArchive
  Create the archive
.PARAMETER ApplyPolicy
  Specified Retention Policy
.PARAMETER StartMFA
  Start the ManageFolderAssistant for the users
  Automatically enabled if the ApplyPolicy switch is selected
.PARAMETER ListRetentionPolicies
  Shows a list of available retention Policies
.PARAMETER Mailbox
  Specify an individual mailbox instead of using the CSV file
.EXAMPLE
  Enable-PersonalArchive.ps1
  Enable the Personal Archive for all the users in C:MigrationListArchivelist.csv
  and applies the "Default Archive and Retention Policy" retention policy
.EXAMPLE
  Enable-PersonalArchive.ps1 -CreateArchive
  Create personal archives using the default retention policy for all the users in C:MigrationListArchivelist.csv
.EXAMPLE
  Enable-PersonalArchive.ps1 -ListRetentionPolicies
  Displays a list of the available retention policies
#>
#########################################################################################
PARAM([String]$CSVin="C:MigrationListArchivelist.csv",
      [String]$RetentionPolicy="Default Archive and Retention Policy",
      [Switch]$ListRetentionPolicies=$false,
      [Switch]$EnableArchive,
      [Switch]$ApplyPolicy,
      [Switch]$StartMFA,
      [String]$Mailbox)
$Error.Clear()
#########################################################################################
$AppName    = "Enable-PersonalArchive.ps1"
$AppVer     = "v1.2 [4 April 2012]"
$ServerName = hostname
$today      = Get-Date
$RunUser    = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).Name
##########################################################################################
#Display script name and version
##########################################################################################
Write-host " " $AppName -NoNewLine -foregroundcolor Green
Write-Host ": " $AppVer -foregroundcolor Green
Write-host "`n Run on $ServerName at $Today by $RunUser" -foregroundcolor Yellow
Write-Host "|——————————————————————-|`n"
Write-Host "PARAMETERS:" -Foregroundcolor Green
Write-host "ListRetentionPolicies:.. " $ListRetentionPolicies
Write-host "EnableArchive:……….." $EnableArchive
Write-host "ApplyPolicy:…………." $ApplyPolicy
Write-Host "StartMFA:……………." $StartMFA
Write-host "CSVin: ………………" $CSVin
Write-host "RetentionPolicy:………" $RetentionPolicy
write-Host "Mailbox:…………….." $Mailbox
##########################################################################################
$xPsCheck = Get-PSSnapin | Select Name | Where {$_.Name -Like "*Exchange*"}
If ($xPsCheck -eq $Null) {Add-PsSnapin Microsoft.Exchange.Management.PowerShell.e2010}
If($ListRetentionPolicies){
  Write-Host "Available Retention Policies" -Foregroundcolor Blue
  Get-RetentionPolicy | select Name
  Exit
} #If($ListRetentionPolicies)
Write-Host ""
If ($Mailbox -eq ""){
  Write-Host "Reading $CSVin"
  $tmpUsers = Import-Csv $CSVin
  $tmpUserCnt =0;$tmpUsers | ForEach{$tmpUserCnt ++}
  Write-Host $tmpUserCnt -Foregroundcolor Green -NoNewLine
  Write-Host " Users found"
}ELSE{
  Write-Host "Individual Mailbox spelected [$Mailbox]"
  $tmpusers = "" | Select email
  $tmpusers.email = $mailbox
} #If ($Mailbox -eq "")
If($EnableArchive -eq $True){
  #First Loop to enable archive
  Write-Host "`nEnabling Archive" -foregroundcolor Yellow
  ForEach($user in $tmpUsers){Write-Host "`n"$user.email; Get-Mailbox $user.email | Enable-Mailbox -Archive}
  Write-Host "`nWaiting 30 Seconds while AD Catches up!`n" -Foregroundcolor Blue
  Sleep 30
} #If($EnableArchive -eq $True)
#Loop to enable policy
IF($ApplyPolicy -eq $True){
  $StartMFA = $True
  Write-Host "`nSetting Retention Policy: " -NoNewLine
  Write-Host $RetentionPolicy -foregroundcolor Yellow
  ForEach($user in $tmpUsers){
    Write-Host $user.email
    Get-Mailbox $user.email | Set-Mailbox -RetentionPolicy $RetentionPolicy
  } #ForEach($user
  Write-Host "`nWaiting 30 Seconds while AD Catches up!`n" -Foregroundcolor Blue
  Sleep 30
} #IF($ApplyPolicy -eq $True)
#Loop to Start the managedfolderassistant
IF($StartMFA -eq $True){
  Write-Host "`nStart-ManagedFolderAssistant: " -foregroundcolor Yellow
  ForEach($user in $tmpUsers){
    Write-Host $user.email
    Get-Mailbox $user.email | Start-ManagedFolderAssistant
  } #ForEach($user
} #IF($StartMFA -eq $True)
#Loop for the results
Write-Host "`n`nResults" -foregroundcolor Yellow
$tmpResults =@()
ForEach($user in $tmpUsers){
  #Write-Host
$user.email
  $tmpResults += Get-Mailbox $user.email | select PrimarySmtpAddress, Database, RetentionPolicy, ArchiveDatabase
} #ForEach($user
$tmpResults | ft -auto
If ($Mailbox -eq ""){
  If ($EnableArchive -or $ApplyPolicy){
    $xBatchName = get-date -Format "yyyy-MMM-dd_HH-mmm-ss"
    $csvRename = ($csvin.split("."))[0] + "_" + $xBatchName + ".csv"
    Write-host "Renaming csv files to $csvrename" -foregroundcolor yellow
    Ren $csvin $csvRename
  } #If ($EnableArchive -or $ApplyPolicy)
} #If ($Mailbox -eq "")
#END
#########################################################################################

Leave a Reply

%d bloggers like this: