Getting Message Back to Caller

Good morning everyone,

Question. Is there a straight forward way to pass an error or general output from a script to a dashboard. For instance, I’d like to display a message on the dashboard if the user is already in a CSV file. My code looks like this,

Invoke-PSUScript 'CSV_Populator.ps1' FormData $EventData -Integrated -Wait

Here is CSV_Populator.ps1,

<#
    .SYNOPSIS
        Comparing Current Date with date set for 'Coverage Until' and 'Transfer Date'
        
    .DESCRIPTION
        Comparing Current Date with date set for 'Coverage Until' and 'Transfer Date'
    #>
param(
    [Parameter(Mandatory)]
    $FormData
)

$FileName = 'C:\PSU_Employee_Transfer\Employee_Transfer_Schedule.CSV'

#Make sure object properties are correct before exporting to CSV.
if ($FormData[0].CoverageSelection -eq 'No'){
    $FormData[0].CoverageDate = 'N/A'
}

#Check to see if CSV exists.
if([System.IO.File]::Exists($FileName)){
    $CSV = Import-CSV -Path $FileName
    if ($FormData[0].SamAccountName -in $CSV.SamAccountName){
        'DISPLAY MESSAGE HERE'
        return $false
    }
    $FormData[0] | Export-Csv -Path $FileName -NoTypeInformation -Encoding UTF8 -Append
}
else{
    $FormData[0] | Export-Csv -Path $FileName -NoTypeInformation -Encoding UTF8
}

The only way I can think of how to do this would be to do $true or $false check, and do something like this on the dashboard,

$Validation = Invoke-PSUScript 'CSV_Populator.ps1' FormData $EventData -Integrated -Wait

if ($Validation -eq $false){
"DISPLAY SOMETHING HERE"
}

There has to be a cleaner way to do this. Would anyone be able to provide insight on this for me? Thank you, and your help is greatly appreciated.

Throwing up a hailmary for a quick fix and tagging @adam :smile:

There isn’t really at the moment but I think that Invoke-PSUScript should probably look at the job status and throw an error if the job fails (if error action is Stop).

In the mean time, you’d likely do something similar to what you suggested. I might return a hashtable so I could provide the validation status and message from the script.

        $Result = Invoke-PSUScript -Name 'Error.ps1' -Integrated -Wait
        if (-not $Result.Valid)
        {
            Show-UDToast $Result.Message
        }
@{
    valid = $false
    message = "You're already in the CSV!"
}

I appreciate your quick response. Thanks Adam! I’ll take what you said and mess around with it a bit.