Importing the session does work and it makes sense that it would be starting a new runspace and require it to be imported again.
Now I have another issue. When the session closes or I cancel the email purge I attempt to remove the session, remove the table, then show a text string and a button but it returns a React error that according to the decoder it is expecting an array?
Objects are not valid as a React child (found: Error: Minified React error #310; visit https://reactjs.org/docs/error-decoder.html?invariant=310 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.). If you meant to render a collection of children, use an array instead.
New-UDTable -Data $Data -Id 'ItemsTable' -Title "Items found in the following mailboxes" -Columns $Columns
New-UDButton -Id "btnPurge" -Text "Purge Emails" -OnClick {
Show-UDModal -Content {
New-UDAlert -Severity 'warning' -Content {
New-UDHtml "Are you sure you want to purge the emails from <strong>$($MailSender.value)</strong>"
New-UDButton -Id btnDelete -Text "Purge" -OnClick {
Import-PSSession $MySession -AllowClobber | Out-Null
Hide-UDModal
if ($MySession -is [System.Management.Automation.Runspaces.PSSession]) {
$Iterations = 0; $ItemsProcessed = 0
While ($ItemsProcessed -lt $ItemsFound) {
$Iterations++
Show-UDToast -Message "Starting the purge... ($Iterations)" -Duration 2000 -BackgroundColor red -MessageColor white
New-ComplianceSearchAction -SearchName $SearchID -Purge -PurgeType SoftDelete -Confirm:$False
While ((Get-ComplianceSearchAction -Identity "$($SearchID)_Purge").Status -ne "Completed") {
Start-Sleep -Seconds 2
Show-UDToast -Message "Purging items..." -Duration 2000 -BackgroundColor red -MessageColor white
}
$ItemsProcessed = $ItemsProcessed + 10
Remove-ComplianceSearchAction -Identity "$($SearchID)_Purge" -Confirm:$False
Show-UDToast -Message "Search action removed." -Duration 2000 -BackgroundColor red -MessageColor white
}
}
Else {
Show-UDModal -Content {
New-UDTypography -Text "No session still exists."
} -FullWidth -MaxWidth 'md'
}
Remove-PSSession -Id $MySession
Remove-UDElement -Id 'ItemsTable'
New-UDTypography -Text 'Purge complete'
New-UDButton -Text "Search Again" -Icon (New-UDIcon -Icon hand_point_left ) -OnClick {
Invoke-UDRedirect -Url "$DBUrl/phrem"
}
}
New-UDButton -Id btnDelCancel -Text "Cancel" -OnClick {
Hide-UDModal
Remove-PSSession -Id $MySession
Remove-UDElement -Id 'ItemsTable'
New-UDTypography -Text 'Purge canceled'
New-UDButton -Text "Search Again" -Icon (New-UDIcon -Icon hand_point_left ) -OnClick {
Invoke-UDRedirect -Url "$DBUrl/phrem"
}
}
} -Title "Warning"
} -Persistent -FullWidth -MaxWidth 'md'
}