Public/SessionManager.ps1

<#
    This SessionManager Script handles creating and receiving output from a Session-Based Runspace Environment
    Output received from actions that are being run in this script are sent to TMConsole via WebSocket a websocket
 
    Messages sent to the WebSocket must be:
        - An JSON string
        - The Json Object must have the following properties:
            - TMTaskId: <int> This is the task.taskNumber (not the taskId)
            - Type:
                One of [
                    'Started' - Expressly creates a 'TaskCache' object which stores the output of a given Task's Action
                        ** Note - The receiving handler does not expressly require a 'Started' token, if any other types are received first
                                  And one doesn't already exist, one is created to store the other Type-d data.
 
                    'Info' - A 'Write-Host' message from the 'info' stream
                    'Progress' - An 'Activity Progress' message from the 'Progress' stream
                    'Failed' - An Error bearing token with a 'Message' property which is displayed in the console
                    'Completed' - A token with a simply indicating that the task is complete and all progress should reflect this
 
                ]
 
    Among others. There is "Info", "Started", "Failed", "Progress" and "Completed"
    Each of the JSON representations are of the associated PowerShell Primative Classes that are emitted by the the script using:
 
    - Started:
        Psuedo message, not actually created by PowerShell, it's written to the StdOut as early as possible (when this script gets the ID for the first time
 
    - Info: Write-Host or any pipeline output (Get-Date, for example)
 
    - Progress: Write-Progress
        ActivityID (Id)
        ParentActivityId
        Activity
        CurrentOpperation
        Status
        PercentComplete
        SecondsRemaining
        RecordType (1 = running, 0=Completed)
 
    - Errors Writtn as "Failed"
        - Errors Thrown from Infrastructure problems (Session issues, network, etc)
        - TM Action Scripts are run by the user are in a try catch and reported this way
 
    - Completed:
        Occurs when the Action Script provided by TM is completed, with no errors
 
    --- TODO
    - Verbose
        Write-Verbose output should be displayed in TMD as well (with a UI switch)
 
 #>



Function Start-TMCSessionManager {
    <#
    .SYNOPSIS
        Executes a new TMConsole PowerShell Server instance to handle
 
    .NOTES
        Name: Start-TMCSessionManager
        Author: TransitionManager
        Version: 1.1
        DateCreated: 2021-08-21
        1.1: 2022-02-09
 
 
    .EXAMPLE
        Start-TMCSessionManager -Hostname <hostname> -Port 8620
 
    .LINK
        https://support.transitionmanager.net
    #>

    param(
        [Parameter(mandatory = $false)]
        [bool]$OutputVerbose = $false, ## PROD SETTING
        # [bool]$OutputVerbose = $true, ## DEV SETTING

        [Parameter()]$HostPID = -1,

        [Parameter()][Bool]$AllowInsecureSSL = $False,
        [Parameter()][Bool]$AllowDirectExecution = $False,

        [Parameter()]
        [String]$WebSocketServer,

        [Parameter()]
        [Int32]$WebSocketPort = 8620,

        [Parameter(Mandatory = $false)]
        [Version]$TMCVersion = '0.0.0'

    )

    Begin {

        ## Enable Verbose Output if OutputVerbose was true
        if ($OutputVerbose) {
            $Global:VerbosePreference = 'Continue'
            $VerbosePreference = 'Continue'
            Write-Verbose -Message "Starting TMConsole Server Session {$($PowershellServerSettings.Hostname):$($PowershellServerSettings.Port)}"
            Write-Verbose -Message ($MyInvocation.BoundParameters | ConvertTo-Json)
        }

        ## Create the powershell WebSocketServer connection settings
        $PowerShellServerSettings = @{
            Hostname = $WebSocketServer.ToLower()
            Port     = $WebSocketPort
        }

        ##
        ## Begin Running the Session Manager
        ##

        ## Allow the TMD Watcher script to continue and move past any transient errors
        $ErrorActionPreference = 'Continue' ## PRODUCTION = 'Continue'. Setting this to 'Stop' is useful in debugging this script

        ## Enable Verbose (using bool, not switch)
        if ($OutputVerbose) {

            $VerbosePreference = 'Continue'

            ## If Host PID is present, notify that SessionManager is bound to that pid
            if ($HostPID -ne -1) {
                Write-Host "VERBOSE: Bound to TMD PID: $HostPID"
            }
        } else {
            $VerbosePreference = 'SilentlyContinue'
        }
    }

    Process {

        ## Error Action
        $ErrorActionPreference = 'Continue'

        ## Define the settings for the web socket client
        $StartWebSocketClientSplat = @{
            WebSocketServer  = $($WebSocketServer.ToLower())
            WebSocketPort    = $WebSocketPort
            Verbose          = $Verbose
            OutputVerbose    = $OutputVerbose
            AllowInsecureSSL = $AllowInsecureSSL
            TMCVersion       = $TMCVersion
        }

        ## Start the WebSocketClient directly
        Start-TMConsoleWebSocketClient @StartWebSocketClientSplat

    }
    End {
        Write-Verbose 'TMConsole.SessionManager has ended'
    }
}