New-UDTableColumn Button on select rows not working as expected

Hello,

I have a table that contains rows of alerts. Once acknowledged, I want the button to disappear, but this logic is not working:

            New-UDTableColumn -Property acknowledge -Title "Acknowledge?" -Render { 
                if ($($EventData.ackBy) -eq '') {
                    New-UDButton -Id "btn$($EventData.alertID)" -Text "Acknowledge Alert" -OnClick {
                        Show-UDToast -Message "Alert $($EventData.alertID) has been acknowledged"
                        Invoke-Sqlcmd -ServerInstance server-Database monitor -Query "UPDATE tblAlerts SET acknowledged = 1, acknowledged_by = '$User', acknowledged_on = GETDATE() WHERE alertID = $($EventData.alertID)"
                    } 
                } else { 
                    New-UDButton -Text "$($EventData.ack)"
                }
            }

No matter what, the else statement always executes. I printed the information on the buttons to see what was displaying and it is showing blank for some or the name of the user who has acknowledged the button.

Product: PowerShell Universal
Version: 2.0.0

I updated with my section of the code instead of an example.
The ackBy is from the database and contains a string (varchar) of the username that acknowledged the alert. I want to not show the button if the alert has been acknowledged, seems like it should be straight foward, but I must be missing something.

Thank you!!

Have you tried the following instead?

If ($($EventData.ackBy) -eq ‘’ -or $($EventData.ackBy) -eq $null)

1 Like

@jyoakum - I did try that and still falls to the else section. I am not sure what exactly is causing it. So for instance, if you look at my buttons, they are blank due to me populating it with that field which is currently empty. I am not sure if @adam can maybe shed some light on this. I just updated to 2.0.3 today to see if that resolves the issue.

The issue is with your script not the env, i would suggest testing your if statement to debug your issue the opposite way to see if it changes like

if ($($EventData.ackBy) -ne '') {
                    New-UDButton -Id "btn$($EventData.alertID)" -Text "Acknowledge Alert" -OnClick {
                        Show-UDToast -Message "Alert $($EventData.alertID) has been acknowledged"
                        Invoke-Sqlcmd -ServerInstance server-Database monitor -Query "UPDATE tblAlerts SET acknowledged = 1, acknowledged_by = '$User', acknowledged_on = GETDATE() WHERE alertID = $($EventData.alertID)"
                    } 
                } else { 
                    New-UDButton -Text "$($EventData.ack)"
                }
            }

and you can also try after that

if (-Not $($EventData.ackBy) ) {
                    New-UDButton -Id "btn$($EventData.alertID)" -Text "Acknowledge Alert" -OnClick {
                        Show-UDToast -Message "Alert $($EventData.alertID) has been acknowledged"
                        Invoke-Sqlcmd -ServerInstance server-Database monitor -Query "UPDATE tblAlerts SET acknowledged = 1, acknowledged_by = '$User', acknowledged_on = GETDATE() WHERE alertID = $($EventData.alertID)"
                    } 
                } else { 
                    New-UDButton -Text "$($EventData.ack)"
                }
            }

Still cannot get the button to go away. I tried using

                    if (-Not $($EventData.ackBy)) {
                        New-UDButton -Id "btn$($EventData.alertID)" -Text "Acknowledge Alert" -OnClick {
                            Show-UDToast -Message "Alert $($EventData.alertID) has been acknowledged"
                            Invoke-Sqlcmd -ServerInstance serverx -Database monitoring -Query "UPDATE tblAlerts SET acknowledged = 1, acknowledged_by = '$User', acknowledged_on = GETDATE() WHERE alertID = $($EventData.alertID)"
                        } 
                    }

Causes the button to not appear at all regardless if there is data in ackBy field. Am I doing something wrong?

Also tried with this code:

                New-UDTableColumn -Property acknowledge -Title "Acknowledge?" -Render { 
                    if(-not($EventData.ackBy -eq $null -or $EventData.ackBy -eq "")) {
                        New-UDButton -Id "btn$($EventData.alertID)" -Text "Acknowledge Alert" -OnClick {
                            Show-UDToast -Message "Alert $($EventData.alertID) has been acknowledged"
                            Invoke-Sqlcmd -ServerInstance server -Database monitoring-Query "UPDATE tblAlerts SET acknowledged = 1, acknowledged_by = '$User', acknowledged_on = GETDATE() WHERE alertID = $($EventData.alertID)"
                        } 
                    } else {
                        ""
                    }
                }

I am not sure if we are allowed to bump our post, but still having issues hiding the button.

You could try to hide the button using Set-UDElement.

  New-UDTableColumn -Property acknowledge -Title "Acknowledge?" -Render { 
                if ($($EventData.ackBy) -eq '') {
                    New-UDButton -Id "btn$($EventData.alertID)" -Text "Acknowledge Alert" -OnClick {
                        Show-UDToast -Message "Alert $($EventData.alertID) has been acknowledged"
                        Invoke-Sqlcmd -ServerInstance server-Database monitor -Query "UPDATE tblAlerts SET acknowledged = 1, acknowledged_by = '$User', acknowledged_on = GETDATE() WHERE alertID = $($EventData.alertID)"
                        Set-UDElement -Id "btn$($EventData.alertID)" -Properties @{ style = @{ display = "none" } } 
                    } 
                } else { 
                    New-UDButton -Text "$($EventData.ack)"
                }
            }

@adam, I tried that and it did not work. In my case, there is no acknowledged by and it ended up displaying a blank button.

            if ($Roles -notcontains 'Monitoring') {
                New-UDTableColumn -Property acknowledge -Title "Acknowledge?" -Render { 
                     if ($($EventData.ackBy) -eq '') {
                        New-UDButton -Id "btn$($EventData.alertID)" -Text "Acknowledge Alert" -OnClick {
                            Show-UDToast -Message "Alert $($EventData.alertID) has been acknowledged"
                            Invoke-Sqlcmd -ServerInstance server
![Capture063021|690x62](upload://kEXcHf1Y6DTlkrkVU9FF6546fn4.jpeg)
 -Database Monitoring -Query "UPDATE tblAlerts SET acknowledged = 1, acknowledged_by = '$User', acknowledged_on = GETDATE() WHERE alertID = $($EventData.alertID)"
                            Set-UDElement -Id "btn$($EventData.alertID)" -Properties @{ style = @{ display = "none" } } 

                        } 
                    } else {
                        New-UDButton -Text "$($EventData.ack)"
                    }
                }
            }

I attached a screenshot. I am just not sure why it keeps falling to the else statement. I know I would want to put “” in the else when I do not want to display anything. At this time, I have it there to see what it is showing.

I was able to get this working by doing this:

                New-UDTableColumn -Property acknowledge -Title "Acknowledge?" -Render { 
                     if ([string]::IsNullOrEmpty($EventData.ackBy)) {

A post on stack overflowed explain this:

https://stackoverflow.com/questions/47128737/why-does-an-empty-powershell-hash-table-evaluate-to-true-when-an-empty-array-eva