O365 - resuming move requests

For resuming move requests outside of business hours

Picks up failed move requests too (if PAW is configured) - https://blogs.technet.microsoft.com/exchange/2017/12/01/paw-your-way-into-office-365-migrations/

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

$startresumetimeinput = Read-host "Specify the time to resume move requests (in hh:mm format)"

$now = [System.DateTime]::Now
$today = [System.DateTime]::Today
$tomorrow = ($today).AddDays(1)

$startresumetime = Get-date $startresumetimeinput

if($now -gt $startresumetime){$startresumetime = ($startresumetime).AddDays(1)}

write-host "`$startresumetime is $startresumetime"


while($startresumetime -gt $now){write-progress -Activity "Waiting to resume moverequests at $($startresumetime)" -Status "Checking every 5 minutes. Time last checked at $($now)"; sleep 300; $now = [System.DateTime]::Now}


$warningPreference = "SilentlyContinue"

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

[System.DateTime]::Now


Write-Progress -Activity "Getting move requests..." -Status "(where status = Suspended or Failed/failing)"

$mrstr = Get-MoveRequest -ResultSize unlimited | ?{$_.Status -eq "Suspended" -or $_.Status -like "*ail*"} | select exchangeguid
[INT]$mrstrcount = ($mrstr).count
Write-host "MoveRequests to be resumed:"$mrstrcount
[INT]$counter = 0
foreach($mrtr in $mrstr){
$counter ++
Write-Progress -Activity "Resuming move requests..." -Status "$counter of $mrstrcount"
[string]$mrexguid = $mrtr.exchangeguid
Resume-MoveRequest $mrexguid -confirm:$false
}

[System.DateTime]::Now

Get-PsSession | Remove-PsSession