internal/scripts/removalEvent.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#region Handle Module Removal
$PSF_OnRemoveScript = {
    # Stop all managed runspaces ONLY on the main runspace's termination
    if ([runspace]::DefaultRunspace.Id -eq 1)
    {
        Wait-PSFMessage -Timeout 30s -Terminate
        Get-PSFRunspace | Stop-PSFRunspace
        [PSFramework.PSFCore.PSFCoreHost]::Uninitialize()
    }
    
    # Properly disconnect all remote sessions still held open
    $psframework_pssessions.Values | Remove-PSSession
}
$ExecutionContext.SessionState.Module.OnRemove += $PSF_OnRemoveScript
Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -Action $PSF_OnRemoveScript
#endregion Handle Module Removal