src/events/Register-Shutdown.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# NoExport: Register-Shutdown
function Register-Shutdown {
    [CmdletBinding(PositionalBinding = $false)]
    Param()

    # callback when Remove-Module is called on this module
    $OnRemoveScript = {

    }
    $ExecutionContext.SessionState.Module.OnRemove += $OnRemoveScript

    # TODO: the Action callback is never called below when 'pwsh' is executed. Perhaps CIM detection
    # is more ideal.
    $IsHistoryRecordingEnabled = (Get-PowerBoltSetting -Name 'TurnOnHistoryRecording') -eq $true
    Register-EngineEvent -SourceIdentifier ([System.Management.Automation.PsEngineEvent]::Exiting) -MessageData $IsHistoryRecordingEnabled -Action {
        if ($Event.MessageData) {
            # Export-History
        } 
        $OnRemoveScript
    } -SupportEvent | Out-Null
}