Issue with PowerShell variables

I just found out about Universal Dashboard yesterday, and I love it.
I may not be using it correctly, though. I have the following sample script:
Import-Module UniversalDashboard

Get-UDDashboard | Stop-UDDashboard

$services = Get-Content 'C:\temp\Services.txt'
$Dashboard = New-UDDashboard -Title "Service Groups" -Content {
    New-UDLayout -Columns 3 -Content {
        foreach ($service in $services) {
            New-UDCard -Title "$($service) services" -Content {
                $criteria = "$($service)*"
                New-UDElement -Tag "div" -Content {"$((Get-Service | ? displayname -like "$($service)*").Count) members in group"}
                New-UDButton -Text "Generate $($service) list" -OnClick {
                    Get-Service | ? displayname -like "$($service)*" |
                                Select Name, DisplayName, status | 
                                Export-Csv -Path c:\temp\outServices.txt -NoTypeInformation
                    Show-UDToast -message  "$($service) member list generated." -Position bottomRight -Duration 3000 
                }
            }
        }
    }
}
Start-UDDashboard -Dashboard $Dashboard -Port 10001

My services.txt file contains the following 3 lines:
Application
Diagnostic
Hyper-V

Here is a screenshot:

When I click on the UDButton, I would like to export the services beginning with the name on the card, and display a toast message. For some reason, $service is being ignored in both the button’s OnClick event and in the toast message.
Any assistance would be appreciated. Thanks!

@sjacobs

Welcome to the UD forum and happy to have another UD lover. :slight_smile:

I am not in front of a computer but I think the problem is the same as in this thread: Pass variable to modal

@augustin.ziegler

Thanks for the response. The thread seems to be saying that I need to add a UDEndpoint to pass a variable to the toast. I’ve tried the following, but the toast message does not appear:

Get-UDDashboard | Stop-UDDashboard

$Dashboard = New-UDDashboard -Title "Parameter Test" -Content {
            $service = "Test Service"
            New-UDCard -Title "$($service)" -Content {
                New-UDButton -Text "$($service) list" -OnClick {
                    New-UDEndpoint -Endpoint {
                        Show-UDToast -message  $ArgumentList[0]
                    } -ArgumentList @($service)
                } 
            } 
}
Start-UDDashboard -Dashboard $Dashboard -Port 10001

I think the problem is that you are using {} instead of () for -OnClick

Try this:

New-UDButton -Text "$($service) list" -OnClick (
                    New-UDEndpoint -Endpoint {
                        Show-UDToast -message  $ArgumentList[0]
                    } -ArgumentList @($service)
                )

Bingo! That was it! Thank you very much!!

1 Like

Is there a way to mark the question as answered? Thanks again!

Not that I am aware of … thought so too about this :+1:

I just enabled the Solution plugin. You’ll be able to select a reply as a solution to a question.

1 Like

Thanks!