Running script on click

I have a dashboard with buttons. Once a button is clicked, a ps1 script is called and executed successfully. How can I structure my dashboard so that when a button is clicked, a new PowerShell Window will open and output the results to the console during script execution?

Product: PowerShell Universal
Version: 2.1.2

You can collect the output with Get-UAJobOutput

Invoke-UAScript -Script 'Script1.ps1' -RequiredParameter 'Hello' | Tee-Object -Variable job | Wait-UAJob
$HostOutput = Get-UAJobOutput -Job $Job

Then you can display that output in a read only Code Editor.

When running the Invoke-UAScript line, I receive the following error: "Cannot retrieve the dynamic parameters for the cmdlet. Call failed.

#Gathering all the data here and storing it in $AllData

$Pages = @()

$Pages += New-UDPage -Name 'CitrixVMs' -Content `
{
    $Columns = @(
                    New-UDTableColumn -Property Server           -Title Server
                    New-UDTableColumn -Property MasterVM         -Title MasterVM
                    New-UDTableColumn -Property Snapshot         -Title Snapshot
                    New-UDTableColumn -Property LastModifiedDate -Title LastModifiedDate
                    New-UDTableColumn -Property Site             -Title Site -Render `
                    {
                        New-UDButton -id "btn$($Site.Name)" -Text "Apply Update" -OnClick {
                                                                                                # OLD METHOD
                                                                                                #Show-UDToast -Message "Updating $($EventData.Site) master image"
                                                                                                #$Script = "C:\Temp\Script1.ps1"
                                                                                                #Invoke-Expression -Command $Script
                                                                                                

                                                                                                $Script = "C:\Temp\Script1.ps1"
                                                                                                Invoke-UAScript -Script $Script -ComputerName $($EventData.Site) | Tee-Object -Variable job | Wait-UAJob
                                                                                                $HostOutput = Get-UAJobOutput -Job $Job

                                                                                          }
                    }
                )

    New-UDTable -Data $AllData -Columns
}


New-UDDashboard -Title 'CitrixVMs' -Pages $Pages

Not sure if this is what you are looking or the answer to your issue/question but I got mine to work by creating an Identity under Security, then create and assign new Token to newly created Identity. Then use the token key to connect to the PSUServer and then call UAScript. Example code below of how is use it. My example below will display on-demand output from the running script into a code editor element.

$AppToken = 'YOUR_TOKEN_HERE'
Connect-PSUServer -ComputerName http://localhost:5000 -AppToken $AppToken

$Job = Invoke-UAScript -Script 'Test.ps1' -service "$Session:svc" -srvname $Session:server -operator $Session:userinfo.displayName

        while($Job.Status -ne 'Completed')
        {
            Start-Sleep 1
            $Output = (Get-UAJobOutput -Job $Job).Data -join ([Environment]::NewLine)
            Set-UDElement -Id 'codeEditor' -Properties @{
                code = $Output
            }

            $Job = Get-UAJob -Id $Job.Id
        }

Get live output from running script reference post

Thanks @adam and @irortiz, I think I’m close. I am now able to execute the .ps1 when clicking the button on the dashboard. I can see the output while logged into the management console of PowerShellUniversal and navigating to Automation > Jobs > output. Am I still missing something to get this output to be displayed on the screen in real-time when the user clicks the button?

The only way I know so far is by using the code editor to display result thru while loop like showed in the example above.

Here is a screenshot of how I implement and display to end-user. Hope this helps :slight_smile:
Oh! and my apologies for line 12. Was frustrated and at the time.

I’m getting an error stating that the ‘New-UDCodeEditor’ is not recognized as the name of a cmdlet even though I have the UniversalDashboard.CodeEditor module installed. I’ll squash that error and post the results once I figure that out. This definitely places me further along! Thanks!

You can verify this by checking under Modules tab. If not there, then you would have to add via Components button. Once that, restart the dashboard.

Correct @irortiz, I’ve confirmed that the UniversalDashboard.CodeEditor component has always been there and it shows up when running “Get-Module” in PowerShell so I’m not sure why the command is still not recognized. Still troubleshooting.

@adam, any thought why I am still receiving the following error message although the CodeEditor module is showing up under Components on the admin page (like the screenshot @irortiz posted) as well as showing as installed in VSCode? The line I’m having issues with is “New-UDCodeEditor -Id ‘codeEditor’ -ReadOnly -Height 500” as you used in Get the output from script "live" in dashboard? - #14 by adam

The term “New-UDCodeEditor” is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.


Figured it out! Although the path to the components show C:\ProgramData\PowerShellUniversal\Dashboard\Components and I confirmed that the module was in that location, it wasn’t until I added the module to “C:\Program Files\WindowsPowerShell\Modules” that the code editor started working in the dashboard and the cmdlet was recognized.

I’m still figuring out what the essential locations are as far as where I need to store the modules in order for them to work properly, but that is for another discussion.

Interesting. You shouldn’t have to do that but I’m glad you were able to get it running.

Final look of the completed, working script. Once the “Apply Update” button is clicked on the dashboard, the “C:\Temp\Script1.ps1” file is executed and displayed within the code editor located on the dashboard.

$Pages = @()

$Pages += New-UDPage -Name 'CitrixVMs' -Content `
{
    $Columns = @(
                    New-UDTableColumn -Property Server           -Title Server
                    New-UDTableColumn -Property MasterVM         -Title MasterVM
                    New-UDTableColumn -Property Snapshot         -Title Snapshot
                    New-UDTableColumn -Property LastModifiedDate -Title LastModifiedDate
                    New-UDTableColumn -Property Site             -Title Site -Render `
                    {
                        New-UDButton -id "btn$($EventData.Site)" -Text "Apply Update" -OnClick {
                                                                                                    Show-UDToast -Message "Updating $($EventData.Site) master image"
                                                                                                    $Script = "C:\Temp\Script1.ps1"
                                                                                                    $Job = Invoke-UAScript -Script $Script2 -Server "$EventData.Server" | Tee-Object -Variable Job

                                                                                                    IF ($Job.Status -eq 'Failed')
                                                                                                    {
                                                                                                        Start-Sleep -Seconds 1
                                                                                                        $Output = (Get-UAJob -Id $Job.Id).StatusDescription -join ([Environment]::NewLine)
                                                                                                        Set-UDElement -Id 'codeEditor' -Properties @{
                                                                                                                                                        code = $Output
                                                                                                                                                    }

                                                                                                        $Job = Get-UAJob -Id $Job.Id
                                                                                                    }

                                                                                                    ELSE
                                                                                                    {
                                                                                                        While ($Job.Status -ne 'Completed')
                                                                                                        {
                                                                                                            Start-Sleep -Seconds 1
                                                                                                            $Output = (Get-UAJob -Id $Job.Id).StatusDescription -join ([Environment]::NewLine)
                                                                                                            Set-UDElement -Id 'codeEditor' -Properties @{
                                                                                                                                                            code = $Output
                                                                                                                                                        }

                                                                                                            $Job = Get-UAJob -Id $Job.Id
                                                                                                        }

                                                                                                    }
                                                                                                     
                                                                                               }
                    }
                )

    New-UDTable -Data $AllData -Columns

    New-UDCodeEditor -Id 'codeEditor' -Theme 'vs-dark' -Height 600 -Language 'powershell' -ReadOnly
}


New-UDDashboard -Title 'CitrixVMs' -Pages $Pages