Pre-FlightChecks.ps1 v1.4

#MsExchange

As promised script #1 of #3

<#
.NOTES
NAME: Pre-FlightChecks.ps1
AUTHOR: Paul Flaherty
Last Edit: v1.4 [11 April 2012]
v1.0 sometime    : A script it born
v1.1 22 Nov 2011 : Added OU to user list output
v1.2 07 Dec 2011 : Added console resize
v1.3 08 Mar 2012 : Added RemoveExistingMoveRequests switch
V1.4 11 Apr 2012 : Add autodiscovery of ADSite
.LINK
blogs.flaphead.com
.SYNOPSIS
This script performs some preflight checks for mailboxes moves
and should be run before a mailbox move
.DESCRIPTION
This script performs a number of checks getting ready for a mailbox move.
The first check is Test-MRSHealth for the a selected set of Hub/Cas servers that
may be involved in the mailbox moves
The second check is to make sure all the mailbox databases are homes on activation
preference #1
The final check read the CSVin file and checks:
  – The mailbox exists
  – If it is in a "valid" state
  – if it has any outstanding move requests
.OUTPUTS
  None
.EXAMPLE
pre-flightchecks.ps1
Run the checks using the default csv file
.EXAMPLE
pre-flightchecks.ps1 -CSVin <csvfile>
Uses the provided CSV file for the list of users
.EXAMPLE
pre-flightchecks.ps1 -RemoveExistingMoveRequests
Uses the default CSV and will remove any existing move requests
.PARAMETER CSVin
  Inpout csv file that contains a list of email addresses and has a single heading of email
.PARAMETER RemoveExistingMoveRequests
This switch will remove existing move requests if they already exist
#>
##########################################################################################
PARAM([String]$CSVin = "C:MigrationListmiguserlist.csv",
      [switch]$RemoveExistingMoveRequests = $False,
      [string]$ExchangeServerPrefix = "*",
      [string]$DatabaseFilter = "*")
$Error.Clear()
CLS
#########################################################################################
$AppName    = "Pre-FlightChecks.ps1"
$AppVer     = "v1.4 [11 Apr 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"
$ADSite = ([System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite()).Name
Write-Host "CSVin:………………….."$CSVin
Write-Host "RemoveExistingMoveRequests:.."$RemoveExistingMoveRequests
Write-Host "ExchangeServerPrefix:…….."$ExchangeServerPrefix
Write-Host "DatabaseFilter:………….."$DatabaseFilter
Write-Host "Current AD Site:…………."$ADSite
$xPsCheck = Get-PSSnapin | Select Name | Where {$_.Name -Like "*Exchange*"}
If ($xPsCheck -eq $Null) {Add-PsSnapin Microsoft.Exchange.Management.PowerShell.e2010}
Write-Host "`n`nChecking MRSHealth" -Foregroundcolor Green
Write-Host "Getting a list of Client Access Servers [$ExchangeServerPrefix]"
$tmpCmdLet = "Get-ExchangeServer $ExchangeServerPrefix | Where{(##_.IsClientAccessServer) -AND (##_.IsE14OrLater)} | Sort Name"
$tmpCmdLet = $tmpCmdLet.Replace("##","$")
$tmpCAS = Invoke-Expression $tmpCmdLet
IF($ExchangeServerPrefix -eq "*"){
  Write-Host "Using ADSite: "$ADSite
  $tmpCAS = $tmpCAS | where {$_.Site -like "*$AdSite*"}
}
ForEach($item in $tmpCAS){
  Test-MRSHealth $item.Name | select Identity, Check, Passed, IsValid, Message | ft -AutoSize -wrap
}
read-host "Review the text above and Press Enter to Continue"
Write-Host "`nChecking Databases [$DatabaseFilter]"  -Foregroundcolor Green
IF($DatabaseFilter  -eq "*"){
  Write-Host "Using ADSite: "$ADSite
  $tmpCmdLet = "Get-ExchangeServer $ExchangeServerPrefix | Where{(##_.IsMailboxServer) -AND (##_.IsE14OrLater)} | Sort Name"
  $tmpCmdLet = $tmpCmdLet.Replace("##","$")
  $tmpMBX = Invoke-Expression $tmpCmdLet
  $tmpMBX = $tmpMBX | where {$_.Site -like "*$AdSite*"}
  $tmpMDB = $tmpMBX | Get-MailboxDatabase | Sort Name -Unique
}ELSE{
  $tmpMDB = Get-MailboxDatabase $DatabaseFilter | Sort Name
}
$tmpMDB | FOREACH {$db=$_.Name; $xNow=$_.Server.Name ;$dbown=$_.ActivationPreference| Where {$_.Value -eq 1};  Write-Host $db "on" $xNow "Should be on" $dbOwn.Key -NoNewLine; If ( $xNow -ne $dbOwn.Key){Write-host " WRONG" -ForegroundColor Red; }ELSE{Write-Host " OK" -Foregroundcolor Green}}
read-host "Review the text above and make sure you have green OK.  Then Press Enter to Continue"
Write-Host "`nChecking Mailboxes"  -Foregroundcolor Green
Write-Host "Using " $csvin
If(test-path $csvin){
  Write-host "$csvin found" -foregroundcolor Green
  $users2Migrate = import-csv $csvin
}ELSE{
  Write-host "$csvin not found" -foregroundcolor red
  exit
} #If(test-path $csvin)
$users = @()
ForEach($xUser in $Users2Migrate){
  $tmpemail = $xUser.email
  Write-Host $tmpemail
  $tUser = Get-Mailbox $tmpemail
  $tmpusers = $tUser | Select PrimarySmtpAddress, @{Expression={$_.Database.Name};Label="Database"}, IsValid, @{Expression={$_.OrganizationalUnit.replace("ad.bgep.co.uk/Offices/","")};Label="OrganizationalUnit"}
  $MR = Get-MoveRequest $tmpemail  -ErrorAction silentlycontinue
  IF($MR -ne $Null){
    Write-Host "!!Outstanding MoveRequest!! .. Run Remove-MoveRequest $tmpemail BEFORE you continue" -foregroundcolor red
    IF($RemoveExistingMoveRequests){Remove-MoveRequest $tmpemail -confirm:$False}
  }
  $users += $tmpUsers
  If ($tmpUsers.IsValid -eq $False){
    Write-Host "`n## Invalid User ##" -Foregroundcolor Red;
    $invalid=$tUser.validate();
    write-host "Property Name:.."$inValid
[0].PropertyName;
    write-host "Description:…."$inValid[0].Description;
    Write-host "######`n"-Foregroundcolor Red
  }
}
$users | ft –auto

#END
#########################################################################################

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: