Display script progress from dashboard

Afternoon all,

I’m not getting any errors or the sorts, more just looking for advice before I move on with my little project.

I have a dashboard that calls a script to performs some AD tasks. I wrote the below within the dashboard to update the end users of what the script is currently up to. I’m wondering if this is the best way to go about this or is there a more acceptable “PSU best practice.” for script progress?

While ($Job.Status -ne "Completed") {
     $JobOutput = Get-PSUJobOutput -Job $Job
     $Job = Get-PSUJob -Id $Job.ID
     
     if ($JobOutput) {
          $Message = $JobOutput[-1]
          # COMPLETED
          if ($JobOutput[-1] -match "COMPLETED") {
               Set-UDElement -Id "ProgressModal" -Content {
                    New-UDAlert -Text "$($Message)" -Severity info
               }
          }
          # GENERAL PROGRESS
          elseif ($JobOutput[-1] -match "PROGRESS") {
               Set-UDElement -Id "ProgressModal" -Content {
                    New-UDAlert -Text "$($Message)" -Severity info
               }
          }
          # ERRORS
          elseif ($JobOutput[-1] -match "ERROR") {
               Set-UDElement -Id "ProgressModal" -Content {
                    New-UDAlert -Text "$($Message)" -Severity Error
               }
          }
     }
     
     Start-Sleep 1
}

I’m just outputting a string from the script with a “Progress message”.

try{
    # SOME TASK
    "PROGRESS MESSAGE"
} catch{
    "ERROR MESSAGE"
}

I’m also a bit worried about the pipeline data building up as this could be run a lot. I won’t be able to discard the the pipeline from that script if I do the above right?

Thanks heaps for any advise!

Why not just use Write-Progress in the script and leverage the built in progress indication it will show on the dashboard automatically?

1 Like

I did try that but I wasn’t able to get it to work. I was likely doing it wrong. I got it working but only using the job statues, not actually updating with what the job is currently doing. If that makes sense.

Bit confused how to use Write-Progress from within the job, not the dashboard I guess.

Thanks heaps for the reply!

Sorry for the delayed reply.

When you implement Write-Progress in your script, and invoke the job for that script from the dashboard with for instance a button, the dashboard will automatically show the values you’ve written in your Write-Progress.

Does that make sense?

1 Like

Hey Ruben,

No problem at all. Thanks for replying at all!

It makes perfect sense (I was over thinking it) but I think this is exactly what I originally tried to do before I messed around for ages and posted here - haha.

I have a dashboard that just has this.

New-UDDashboard -Title 'PowerShell Universal' -Content {
    New-UDButton -Text "Testing" -OnClick {
        Invoke-PSUScript -Script 'TestScript.ps1' 
    } 
}

TestScript.ps1 contains the example from the doco

1..100 | ForEach-Object {
    Write-Progress -PercentComplete $_ -Activity 'Processing...' -CurrentOperation "User $_"
    Start-Sleep -Milliseconds 500
}

Am I doing this right? I am not seeing any progress pop-up. I have tried a few other things before starting on what I originally posted here. Things like piping to Wait-PSUJob, Getting the job output with Get-PSUJob and I have confirmed that $ProgressPreference is set to Continue.

When I press the button, I am not getting any progress pop-up, but the job is starting and if I set -ShowLoading on the button it works as you’d expect.

Any help to point out what I’m doing wrong would be greatly appreciated!
Thanks!

I think, but am not 100% sure on it, but I think you’d have to do;

Invoke-PSUScript -Script 'TestScript.ps1' -Wait

Keep in mind that it does take a little while for the progress to actually show on the dashboard (from what I’ve experienced with it) so short jobs might be completed before it actually triggers?
Perhaps @adam is able to add some concrete information on this feature?
As far as I’m aware, it’s not documented anywhere…

Otherwise you could do something like this;

New-UDDynamic -Id 'dynamic_SyncJobIndicator' -AutoRefresh -AutoRefreshInterval 5 -Content {
    $SyncJob = Get-PSUJob -Script (Get-PSUScript -Name 'NCE-Manager\Invoke-SyncJob.ps1') -First 1
    if ($SyncJob.status -eq 'Running') {
        New-UDAlert -Severity info -Title "Sync is currently running! ($($SyncJob.PercentComplete)%)" -Content {
            New-UDIcon -Icon 'Spinner' -Spin
            " $($SyncJob.CurrentOperation)"
        }
    }
}

Oh and finally, make sure you don’t have “Disable Interactive Host” setting enabled for your dashboard :wink:

3 Likes

Your alterative solutions works (I should have paid closer attention to the attributes the job gives you haha, completely missed PercentComplete! ) so thanks heaps for the suggestion! I’ll use this if I can’t get the integration working!

However, even with the `Invoke-PSUScript -wait` I cannot see any of the interactive pop-ups. I left everything the same as the post above but just added the `-Wait` switch to the invoke command.

Important: I got it working before I posted the above. I’ll leave what I experienced just in case someone else finds themselves in the same spot.

I had two issues (At minimum): One being the -Wait was required by the looks of it but the second issue was that I had the script running under a different environment then the Dashboard. I tried setting them both to PS7 envi but no luck, it works when I set both the Dashboard and script to the Default envi or a PS5 envi.

Thanks so much for your help @ruben!

1 Like

Glad I was able to help!