New-UDGrid, scriptBlock and New-UDButton

Hi everybody,

I’m trying to format a grid with remote windows services and for each service, i add a button to start or stop it, but it’s not working. When i try the code with my local computer : it’s works. When i try the code in a scriptblock, i’ve this message " The term ‘New-UDButton’ is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again"

Local computer :

   New-UDGrid -Title "Services" -Endpoint {
   
     
       Get-Service | select Name, DisplayName, Status | ForEach-Object {
             [PSCustomObject] @{
                 "Name" = $_.Name
                 "Display Name" = $_.DisplayName
                 "Status" = [string]$_.Status
				 "Actions"=(New-UDButton -Text "Start" -Icon play -IconAlignment right -OnClick {Show-UDToast -Message "Clicked!"})

             }
        } | Out-UDGridData

}

Network computer :

   New-UDGrid -Title "Services" -Endpoint {
   $session = New-PSSession -HostName administrator@192.168.92.143
   $a = Invoke-Command -Session $session -ScriptBlock {
     
       Get-Service | select Name, DisplayName, Status | ForEach-Object {
             [PSCustomObject] @{
                 "Name" = $_.Name
                 "Display Name" = $_.DisplayName
                 "Status" = [string]$_.Status
				 "Actions"=(New-UDButton -Text "Start" -Icon play -IconAlignment right -OnClick {Show-UDToast -Message "Clicked!"})

             }
			 }
			 }| Out-UDGridData
		
        }

How i can do this please ?

@James971
you need to use the invoke-command inside the button scriptblock not before. when you use invoke-command you are now under the remote computer shell not the source so in your case the remote computer does not have universal dashboard installed so it didn’t recognizes the new-udbutton .

so do it this way

  New-UDGrid -Title "Services" -Endpoint {
   $session = New-PSSession -HostName administrator@192.168.92.143
   $a = Invoke-Command -Session $session -ScriptBlock {
     
       Get-Service | select Name, DisplayName, Status }

 ForEach($_ in $a) {
             [PSCustomObject] @{
                 "Name" = $_.Name
                 "Display Name" = $_.DisplayName
                 "Status" = [string]$_.Status
				 "Actions"=(New-UDButton -Text "Start" -Icon play -IconAlignment right -OnClick {Show-UDToast -Message "Clicked!"})

             }
			 } | Out-UDGridData
		 
	
    }

@ wsl2001 : Thanks for your reply. It’s resolved the problem with the button, but I no longer have the list of the remote windows services . Now I just have one item.

New-UDGrid -Title “Services” -Endpoint {
$session = New-PSSession -HostName administrator@192.168.92.143
$a = Invoke-Command -Session $session -ScriptBlock { Get-Service | select Name, DisplayName, Status }

data = ForEach(_ in a) { [PSCustomObject] @{ "Name" = .Name
“Display Name” = _.DisplayName "Status" = [string]
.Status
“Actions”=(New-UDButton -Text “Start” -Icon play -IconAlignment right -OnClick {Show-UDToast -Message “Clicked!”})

         }
		 } $data | Out-UDGridData

}

I added $data and my problem is solved now. Thanks