If you’re on 2.5.5, you can actually simplify this and remove the use of app tokens. We added an -Integrated switch to some cmdlets to make executing jobs easier from APIs and dashboards.
No need to call connect or setup app tokens.
Invoke-UAScript -ID $script.ID -FileName "$SessionId.csv" -Integrated | Tee-Object -Variable | Wait-PSUJob -Integrated
$Pipeline = Get-PSUJobPipelineOutput -Job $Job -Integrated
$HostOutput = Get-PSUJobOutput -Job $Job -Integrated
$HostOutput.Data