Public/Invoke-TMDebugActionRequest.ps1

Function Invoke-TMDebugActionRequest {
    <#
    .SYNOPSIS
        This function accepts a TransitionManager Action Request and queues it for execution
 
 
    .NOTES
        Name: Invoke-TMActionRequest
        Author: TransitionManager
        Version: 1.0
        DateCreated: 2021-04-05
 
 
    .EXAMPLE
        Invoke-TMActionRequest -ActionRequestB64 $Base64EncodedActionRequest
 
 
    .LINK
        https://support.transitionmanager.net
    #>


    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 0
        )]
        [string]  $ActionRequestFilePath
    )

    BEGIN {}

    PROCESS {

        ## Setup an apropriate quiet environment
        $global:InformationPreference = 'Continue'
        $global:ProgressPreference = 'SilentlyContinue'
        $global:VerbosePreference = 'SilentlyContinue'
        $global:DebugPreference = 'SilentlyContinue'
        $global:WarningPreference = 'SilentlyContinue'
        $global:ErrorActionPreference = 'Continue'         ## Using 'Stop' will break execution and not throw errors to the next handler. Don't release it that way.
        $global:ConfirmPreference = 'None'              ## Allows functions that would typically require confirmation to be overridden
        $global:PSModuleAutoloadingPreference = 'All'      ## Enable Auto Module Imports if commands match

        ## Test to see if we already have an ActionRequest object. If so, this session has already been run and the setup is no longer needed.
        if (-not $actionRequest) {

            ##
            ## Setup the Shell and Session
            ##

            ## The remote session had previously imported some importent detail, this will do it here in this session.
            . Import-TMDActionRequest -FromFile $ActionRequestFilePath

            ## Get the Task File Name
            if ($IsWindows) {
                $TaskDebugFolderPath = Join-Path -Path 'C:\Users' -ChildPath $env:USERNAME -AdditionalChildPath 'TMD_Files', 'debug'
                $TaskDebugFilePath = (Join-Path $TaskDebugFolderPath ('Task_' + $actionRequest.task.taskNumber + '.ps1'))
            }

            # Print Debug Header
            Write-Host "Beginning Debug Session for:"
            Write-Host " Task #: "$actionRequest.task.taskNumber -ForegroundColor Green
            Write-Host " Action: "$actionRequest.options.apiAction.name -ForegroundColor Green
            Write-Host ""
            Write-Host 'Variable [$Params]: '
            $Params.PSObject.Properties | Where-Object {
                $_.Name -notlike 'Dt*'
            } | Select-Object Name, Value | Out-String

            New-Variable -Name 'ActionProvider' -Value $actionRequest.task -Force -Scope Global

            ## Set a breakpoint to give the user control over how the file is run. (Only the first time.)
            if ((Get-PSBreakpoint).Count -lt 1) {

                Set-PSBreakpoint -Script $TaskDebugFilePath -Line 1 | Out-Null
            }
        }

        ## Now that the Session has been configured and the ActionRequest imported, Run the User's script
        . $TaskDebugFilePath

        ## Mark the task as complete in TM
        Complete-TMTask -ActionRequest $ActionRequest @DataOption

    }

    END {}
}