O365 - suspending move requests

For suspending move requests during business hours (only those below 94%)

Bear these in mind:

The script has to produce a move request statistics report which takes an hour per 1500 requests.
Suspending is performed at a rate of 6000 per hour.
Work backwards from the cutoff time (with an emergency window allowed for).
E.g. Move requests = 3000
Business hours start at 09:00
Emergency cutoff  = 08:00
Suspend time = 07:30
Start report time = 05:00 (allow an extra bit of time between report and suspend to be safe)

EMS:

[string]$ExchOnline = "outlook.office365.com"
$exolusername = "#####@#####.onmicrosoft.com"
$exolpassword = Get-Content "C:\TEMP\EncryptedPasswords\#####.txt" | ConvertTo-SecureString  
$Excredential = new-object -typename System.Management.Automation.PSCredential -argumentlist $exolusername, $exolpassword
$EXOnlineSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://$ExchOnline/powershell-liveid/ -Credential $Excredential -Authentication Basic -AllowRedirection -ErrorAction Stop

$startreporttimeinput = Read-host "Specify the time to run a report (in hh:mm format)"

$startsuspendtimeinput = Read-host "Also state what time mailbox moves should be suspended (in hh:mm format)"
$now = [System.DateTime]::Now
$today = [System.DateTime]::Today
$tomorrow = ($today).AddDays(1)

$startreporttime = Get-date $startreporttimeinput

$startsuspendtime = Get-date $startsuspendtimeinput
if($now -gt $startreporttime){$startreporttime = ($startreporttime).AddDays(1); $startsuspendtime = ($startsuspendtime).AddDays(1) }

write-host "`$startreporttime is $startreporttime"

write-host "`$startsuspendtime is $startsuspendtime"

while($startreporttime -gt $now){write-progress -Activity "Waiting to start report at $($startreporttime)" -Status "Time last checked at $($now)"; sleep 60; $now = [System.DateTime]::Now}


[string]$DateTime = (Get-Date).Tostring("dd-MM-yyyy_HHmmss")

[string]$FilePath = "C:\Temp\"
[string]$OutputFilename1 = [string]::Format($FilePath + "MoveRequest_Full_Report_{0}.csv",$DateTime)
[System.DateTime]::Now

$warningPreference = "SilentlyContinue"

Import-PSSession $EXOnlineSession -warningaction silentlycontinue  -erroraction silentlycontinue | Out-Null
$warningPreference = "Continue"

Write-host "Gathering all move request information..."

$mrs = Get-MoveRequest -ResultSize unlimited | ?{$_.Status -notlike "Complet*"}
[INT]$mrscount = ($mrs).count
$moveresults = @()
$remainingmoveresults = @()
[INT64]$totaldata = 0
[INT64]$remainingdatavol = 0
$allmovestats = @()
[int64]$alltransferreddata = 0
[INT]$counter = 0
[System.DateTime]::Now
Write-host "The move requests will now be analyzed for more information..."
foreach($mr in $mrs){
$counter ++
Write-Progress -Activity "Gathering move request statistics..." -Status "$counter of $mrscount"
[String]$ExchangeGUID = $mr.ExchangeGUID
$mrstats = Get-MoveRequestStatistics $ExchangeGUID
[int64]$TotalMailboxSizeBytes = $mrstats.TotalMailboxSize -replace "(^.*?\()|\D"
[INT64]$TotalMailboxSizeMB = $TotalMailboxSizeBytes /1048576
[INT64]$TotalMailboxSizeGB = $TotalMailboxSizeMB /1024
[INT64]$TotalMailboxSizeGBrounded =[math]::Round($TotalMailboxSizeGB,2)
[int64]$TransferredBytes = $mrstats.BytesTransferred -replace "(^.*?\()|\D"
[INT64]$TransferredMB = $TransferredBytes /1048576
[INT64]$TransferredGB = $TransferredMB /1024
[INT64]$TransferredGBrounded =[math]::Round($TransferredGB,2)
#$RemainingGBtoTransfer = $TotalMailboxSizeGB - $TransferredGB
#if($RemainingGBtoTransfer -lt 0){$RemainingGBtoTransfer = 0}
#[INT64]$RemainingGBtoTransferrounded =[math]::Round($RemainingGBtoTransfer,2)
[INT]$TotalMailboxItemCount = $mrstats.TotalMailboxItemCount
[INT]$ItemsTransferred = $mrstats.ItemsTransferred
[INT]$ItemsRemaining = (($TotalMailboxItemCount) - ($ItemsTransferred))
if($ItemsRemaining -lt 0){$ItemsRemaining = 0}
$i = New-Object -TypeName PSObject
$i | Add-Member -MemberType NoteProperty -Name Alias -Value $mrstats.Alias
$i | Add-Member -MemberType NoteProperty -Name PrimarySMTPAddress -Value $mrstats.Alias
$i | Add-Member -MemberType NoteProperty -Name BatchName -Value $mrstats.BatchName
$i | Add-Member -MemberType NoteProperty -Name TotalMailboxSizeGB -Value $TotalMailboxSizeGBrounded
$i | Add-Member -MemberType NoteProperty -Name TransferredGB -Value $TransferredGBrounded
$i | Add-Member -MemberType NoteProperty -Name PercentComplete -Value $mrstats.PercentComplete
$i | Add-Member -MemberType NoteProperty -Name TotalMailboxItemCount -Value $TotalMailboxItemCount
$i | Add-Member -MemberType NoteProperty -Name ItemsTransferred -Value $ItemsTransferred
$i | Add-Member -MemberType NoteProperty -Name ItemsRemaining -Value $ItemsRemaining
$i | Add-Member -MemberType NoteProperty -Name OverallDuration -Value $mrstats.OverallDuration
$i | Add-Member -MemberType NoteProperty -Name TotalInProgressDuration -Value $mrstats.TotalInProgressDuration
$i | Add-Member -MemberType NoteProperty -Name TotalSuspendedDuration -Value $mrstats.TotalSuspendedDuration
$i | Add-Member -MemberType NoteProperty -Name ExchangeGuid -Value $mrstats.ExchangeGuid
$moveresults += $i
$totaldata = (($totaldata) + ($TotalMailboxSizeGB))
[INT64]$alltransferreddata = (($alltransferreddata) + ($TransferredGB))
}
Write-host "Analysis complete. Report saved to"$OutputFilename1
[System.DateTime]::Now

Get-PsSession | Remove-PsSession


$moveresults | sort PercentComplete | export-csv $OutputFilename1 -NoTypeInformation

$moverequeststosuspend = $moveresults | ?{$_.PercentComplete -ne 95}
[INT]$moverequeststosuspendCount = ($moverequeststosuspend).Count

$timenow = [System.DateTime]::Now

while($startsuspendtime -gt $now){write-progress -Activity "Waiting to suspend $moverequeststosuspendCount move requests at $($startsuspendtime)" -Status "Checking once per minute. Time last checked at $($now)"; sleep 60; $now = [System.DateTime]::Now}

write-progress -Activity "Waiting to suspend $moverequeststosuspendCount move requests at $($startsuspendtime)" -Status "Completed" -Completed


[System.DateTime]::Now


$warningPreference = "SilentlyContinue"

Import-PSSession $EXOnlineSession -warningaction silentlycontinue  -erroraction silentlycontinue | Out-Null
$warningPreference = "Continue"

[INT]$counter = 0

Write-host "Suspending $moverequeststosuspendCount move requests..."
foreach($moverequesttosuspend in $moverequeststosuspend){
$counter ++
Write-Progress -Activity "Suspending $moverequeststosuspendCount move requests..." -Status "$counter of $moverequeststosuspendCount"
[String]$ExchangeGUID2 = $moverequesttosuspend.ExchangeGUID
Suspend-MoveRequest $ExchangeGUID2 -confirm:$false
}
[System.DateTime]::Now
Get-PsSession | Remove-PsSession