Move-RequestFromCSV.ps1 v1.5

#MSExchange

I know, I should have posted this a few weeks back .. w h a t e v e r

So this is Script #2 of my 3 script process to move mailboxes.

The process is here: Moving Mailboxes the easy way and script #1 is here: Pre-FlightChecks.ps1 v1.4

<#
.NOTES
NAME: Move-RequestFromCSV.ps1
AUTHOR: Paul Flaherty
Last Edit: v1.5 [8 April 2012]
v1.0 sometime    : A script it born
v1.1 20 May 2011 : Remove monitoring bit from script
v1.2 01 Jul 2011 : Updated to take the BatchName and CSVin as Parameters
v1.3 12 Oct 2011 : Updated with auto switch to skip the read-host question
v1.4 13 Jan 2012 : Updated with BadItemLimit and MRSServer switch
v1.5 08 Apr 2012 : Added Database switch
                    Due to some issue running the monitor script at the end, the script now sets
                    an Environment Variable that the monitor script can use.
.LINK
blogs.flaphead.com
.SYNOPSIS
This script creates Move Requests from a csv file
.DESCRIPTION
This script imports a list of smtp addresses from a CSV file and generates new move requests.
The move requests have either an auto generated or manual batch name assosciated with them.
If the CSV file that is being used has two columns: Email and TargetDatabase, the script
will use the TargetDatabase as the destination of the move request.
If no TargetDatabase is specified it will let Exchange do what it does best an decide
.OUTPUTS
  None
.EXAMPLE
Move-RequestFromCSV.ps1
Run the checks using the default csv file
.EXAMPLE
Move-RequestFromCSV.ps1 -Auto
Run the checks using the default csv file, but skips the "press any key" prompt once the
user validation is complete
This is specially useful if the script is used from either a batch file or scheduled task
.EXAMPLE
Move-RequestFromCSV.ps1 -CSVin <csvfile>
Uses the provided CSV file for the list of users
.EXAMPLE
$DB=@();Get-MailboxDatabase DATABASE.UK07* | ForEach{$db += $_.name}
Move-RequestFromCSV.ps1 -Database $db
This will use the Databases found in $db and round robin the move request to the databases
.PARAMETER CSVin
Input csv file that contains a list of email addresses and has a single heading of email
Optionally, if the CSV file contains an additional heading of TargetDatabase the script
will use this value to specify the target database for the move
.PARAMETER BatchName
This switch will remove existing move requests if they already exist
.PARAMETER Auto
Enabling this switch stops the need to "press any key" once the users have  been checked
.PARAMETER BadItemCount
This is the BadItemCount for MoveRequests
Default value is 30
.PARAMETER MRSServer
If needed you can specify a specific MRSServer to use
.PARAMETER Database
Use this parameter to select the databases you want the moverequest to use.
The script will use the list of databases in round robin stylie for each of the
move requests
#>
##########################################################################################
PARAM([String]$CSVin="C:MigrationListmiguserlist.csv",
      [String]$BatchName,
      [Switch]$Auto=$False,
      [String]$BadItemLimit=30,
      [String]$MRSServer="",
      [String]$Database="")
$Error.Clear()
CLS
#########################################################################################
$AppName = "Move-RequestFromCSV.ps1"
$AppVer  = "v1.5 [8 April 2012]"
##########################################################################################
#Load Common Variables
$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"
$xPsCheck = Get-PSSnapin | Select Name | Where {$_.Name -Like "*Exchange*"}
If ($xPsCheck -eq $Null) {Add-PsSnapin Microsoft.Exchange.Management.PowerShell.e2010}
If ($BatchName -ne ""){
  $xBatchName = $BatchName
} ELSE {
  $xBatchName = get-date -Format "yyyy-MMM-dd_HH-mmm-ss"
  $xBatchName = "UK-" + $xBatchName
}
Write-Host "Batch Name:….$xBatchName"
Write-Host "BadItemLimit:..$BadItemLimit"
Write-Host "MRSServer:…..$MRSServer"
Write-Host "Database:……$Database"
$UserList   = @()
$doMove     = $True
Write-Host "Checking to see if $csvin exists"
If(test-path $csvin){
  Write-host "$csvin found" -foregroundcolor Green
}else{
  Write-host "$csvin not found" -foregroundcolor red
  exit
}
$users2Migrate = import-csv $csvin
Write-Host "Checking Users .."
ForEach($yUser in $Users2Migrate){
  $tmpemail = $yUser.email
  $xUser = "" | Select Name, Email, IsValid, MoveStatus, ActualStatus, PercentComplete, Duration, MailboxSize, ItemCount, BadItemsEncountered, SourceDatabase, TargetDatabase, MRSServerName, Transferred, TransferredPerMinute
  #Check User is Valid
  $tmpmbx = Get-Mailbox $tmpemail
  $tmpmbx
  $xUser.Name = $tmpmbx.name
  $xUser.Email = $tmpemail
  $xUser.IsValid =  $tmpmbx.isvalid
  $xUser.TargetDatabase = $yUser.TargetDatabase
  if ($tmpmbx.isvalid -eq $True){$xUser.MoveStatus = "Unknown"}ELSE{$xUser.MoveStatus = "ERR";$xUser.IsValid = $False}
  $UserList += $xUser
}
$tmpInvalid = $UserList | Where {$_.IsValid -eq $False}
$tmpValid   = $UserList | Where {$_.IsValid -ne $False}
Write-Host "`nThe following users are InValid and will not be migrated" -Foregroundcolor Red
$tmpInvalid | Select Email
Write-Host "`nThe following users will be migrated" -Foregroundcolor Green
$tmpValid | Select Email, TargetDatabase
If($aut
o -eq $False){read-host "Press Enter to Generate Move Requests or CTRL C to exit"}
If ($Database -ne ""){
  $tmpDatabaseArray = $database.split(" ")
  $i=0
  $DatabaseHashTable = @{}
  ForEach($item in $tmpDatabaseArray){; $DatabaseHashTable.Add($i,$item); $i++}
}
Write-Host "Creating Move Requests"
Write-Host "BadItemLimit: "$BadItemLimit
$i=0
$UserList | Where {$_.IsValid} | ForEach{
  $tmpemail = $_.Email
  $tmpTargetDatabase = $_.TargetDatabase
  Write-Host $tmpname " – " $tmpemail
  $tmpcmdlet = "Get-Mailbox $tmpemail | New-MoveRequest -BatchName $xBatchName -BadItemLimit:$BadItemLimit"
  If ($MRSServer -ne ""){Write-Host "MRSServer: $MRSServer"; $tmpcmdlet += " -MRSServer $MRSServer "}
  If ($Database -ne ""){
    $tmpDatabase = $tmpDatabaseArray[$i]
    Write-Host "Database: $tmpDatabase";
    $tmpcmdlet += " -TargetDatabase $tmpDatabase"
    If($i -ge ($DatabaseHashTable.count)-1){$i=0}ELSE{$i++}
  }ELSE{
    If($tmpTargetDatabase -ne $null){    $tmpcmdlet += " -TargetDatabase $tmpTargetDatabase "}
  }
  Invoke-Expression $tmpcmdlet
}
Get-MoveRequest -BatchName $xBatchName
$csvRename = ($csvin.split("."))[0] + "_" + $xBatchName + ".csv"
Write-host "Renaming csv files to $csvrename" -foregroundcolor yellow
Ren $csvin $csvRename
Write-Host "Waiting 30 Seconds while AD Catches up!"
Sleep 30
Write-Host "You can now run:"
Write-Host "C:PSMigrationMonitor-MoveRequest.ps1 "$xBatchName
Write-Host "-or-"
Write-Host "C:PSMigrationMonitor-MoveRequest.ps1"
[environment]::SetEnvironmentVariable(‘BatchName’, $xBatchName,’User’)
[environment]::GetEnvironmentVariable(‘BatchName’,’User’)
#End

Leave a Reply

%d bloggers like this: