Public/ps1/Job/Get-ApprxrRemoteJob.ps1
|
<#
.SYNOPSIS Retrieves and executes a remote job for a specified Apprxr pipeline. .DESCRIPTION Fetches a job assignment from the remote server for the given pipeline, processes the job, and logs the result. Supports multiple job types: - SQL: Executes a SQL job using Start-ApprxrJobSql - FILE: Handles file jobs using Start-ApprxrJobFile - HTTP: Executes HTTP jobs using Start-ApprxrJobHtml - START_PIPELINES: Starts all Apprxr pipelines - STOP_PIPELINES: Stops all Apprxr jobs - GET_PIPELINES: Retrieves all pipeline names - CREATE_PIPELINE: Creates a new pipeline job - SELFUPDATE: Triggers Apprxr module upgrade - GET_VERSION: Returns the Apprxr module version - POWERSHELL: Executes arbitrary PowerShell code (if provided) Logs all actions and sends results back to the remote server. .PARAMETER pipeline The name of the pipeline to retrieve a job for. Use 'Default' for the default channel. .EXAMPLE Get-ApprxrRemoteJob -pipeline 'Pipeline1' # Retrieves and executes a remote job for the 'Pipeline1' channel. .EXAMPLE Get-ApprxrRemoteJob -pipeline 'Default' # Retrieves and executes a remote job for the default channel. .NOTES - Used for remote job management and execution in Apprxr automation. - Handles multiple job types and actions as described above. - Sends job results back to the remote server via Get-ApprxrResult. - Logs all received jobs and actions for auditing. - Errors are returned in the result object and logged. #> function Get-ApprxrRemoteJob{ param ($pipeline) if (-not [string]::IsNullOrEmpty($pipeline) -and $pipeline -ne "Default") { $channelId = "?channelId=$($pipeline)" } $deliverResult = "api/services/app/RemoteControl/SetDeliveries" $getTask= "api/services/app/RemoteControl/GetAssignment$channelId" #Get the job from the server $job = (Get-ApprxrResult -request $getTask).result if (-not $job){ return } try { Log("Receive the following job: $job") if ($job.Action -eq "SQL") { $result = Start-ApprxrJobSql -taskInformation $job $result | Add-Member -MemberType NoteProperty -Name "Id" -value $job.Id -Force -ErrorAction SilentlyContinue } elseif ($job.Action -eq "FILE") { $result = Start-ApprxrJobFile -Base64File ($job.arguments.Base64File) -FileLocation ($job.arguments.FileLocation) -Mode ($job.arguments.Mode) -FileName ($job.arguments.FileName) $result | Add-Member -MemberType NoteProperty -Name "Id" -value $job.Id -Force -ErrorAction SilentlyContinue } elseif ($job.Action -eq "HTTP") { $result = Start-ApprxrJobHtml -taskInformation $job $result | Add-Member -MemberType NoteProperty -Name "Id" -value $job.Id -Force -ErrorAction SilentlyContinue } elseif ($job.Action -eq "START_PIPELINES") { $result = [PSCustomObject]@{ success = $true message = Start-ApprxrPipelines } $result | Add-Member -MemberType NoteProperty -Name "Id" -value $job.Id -Force -ErrorAction SilentlyContinue } elseif ($job.Action -eq "GET_PIPELINES") { $result = [PSCustomObject]@{ success = $true message = Get-ApprxrPipelines } $result | Add-Member -MemberType NoteProperty -Name "Id" -value $job.Id -Force -ErrorAction SilentlyContinue } elseif ($job.Action -eq "STOP_PIPELINES") { $result = [PSCustomObject]@{ success = $true message = Stop-ApprxrJobs } $result | Add-Member -MemberType NoteProperty -Name "Id" -value $job.Id -Force -ErrorAction SilentlyContinue } elseif ($job.Action -eq "CREATE_PIPELINE") { $result = [PSCustomObject]@{ success = $true message = Create-ApprxrPipelineJob } $result | Add-Member -MemberType NoteProperty -Name "Id" -value $job.Id -Force -ErrorAction SilentlyContinue } elseif ($job.Action -eq "SELFUPDATE") { $result = [PSCustomObject]@{ success = $true message = Start-ApprxrUpgrade } $result | Add-Member -MemberType NoteProperty -Name "Id" -value $job.Id -Force -ErrorAction SilentlyContinue } elseif ($job.Action -eq "GET_VERSION") { $module = Get-Module -Name Apprxr -ListAvailable | Select-Object -First 1 $version = if ($module) { $module.Version.ToString() } else { 'Unknown' } $result = [PSCustomObject]@{ success = $true message = $version } $result | Add-Member -MemberType NoteProperty -Name "Id" -value $job.Id -Force -ErrorAction SilentlyContinue } elseif ($job.Action -eq "LOGFILE") { #$lines = if ($job.arguments.Lines) { $job.arguments.Lines } else { 50 } $lines = 250 $logContent = [string](Get-ApprxrLog -Tail $lines | Out-String) $result = [PSCustomObject]@{ success = $true message = $logContent Id = $job.Id } } elseif ($job.Action -like 'POWERSHELL' -and $job.Command) { try { $psResult = Invoke-Expression $job.Command $result = [PSCustomObject]@{ success = $true message = $psResult executedCommand = $job.Command Id = $job.Id } } catch { $result = [PSCustomObject]@{ success = $false message = ("PowerShell command failed: " + $_) executedCommand = $job.Command } } } else { $result = [PSCustomObject]@{ success = $false message = ("There is no handler known for given action: " + $job.Action) } } } catch { $result = [PSCustomObject]@{ success = $false message = ("There is a unhandled error: " + $_) } } # Send result to server $result | Add-Member -MemberType NoteProperty -Name "CreationDate" -value (Get-Date).ToString("o") Get-ApprxrResult -request $deliverResult -body ($result | ConvertTo-Json) Remove-Variable result } |