Updating a monitor from a select-box - what am I missing?


#1

Windows7, Powershell 5.0.10514.6, UniversalDashBoard.Community 2.2.1

Hi all

I’d hoped I would find this easy, but been scratching my head… I want to provide a few server names in a dropdown/select box, and query some basic performance stats from the selected one. This will be for 2nd line support to use. No matter what I do, it appears that the ComputerName parameter being passed to Get-Counter is blank? Code enclosed, hopefully it’s something simple. Been reading the docs, and perusing the examples, but no joy.

It /seems/ that although my toast notification is showing that the Sync-UDElement is updating the UDMonitor (I get a toast notification from the monitor when I change the dropdown), that perhaps the Get-Counter was evaluated at instantiation time, and is thus never changing or being re-evaluated?

I’m aware that $Session:Server will be blank to start, but that’s no big deal.

Have pared-back my example as much as possible - hope it’s a simple one ! Thanks for looking.

If (-Not (Get-Module UniversalDashboard.Community -ErrorAction SilentlyContinue)) {
    Import-Module 'C:\Program Files\WindowsPowerShell\Modules\UniversalDashboard.Community\'
}
Start-UdDashboard -Content {
    New-UdDashboard -Title "Performance Dashboard" -Color '#FF050F7F' -Content {
        New-UDRow -Columns {
            New-UDColumn -Size 4 -Content {
                New-UDSelect -Label "Servers" -Option {
                    New-UDSelectOption -Name "server1" -Value "server1"
                    New-UDSelectOption -Name "server2" -Value "server2"
                } -OnChange {
                    $Session:Character = $EventData
                    Show-UDToast -Message "Changed to $($eventdata)" -Duration 5000
                    $Session:Server = $eventdata
                    Sync-UDElement -Id "dql"
                    Sync-UDElement -Id "proc"
                    Sync-UDElement -Id "disktime"
                }
            }
        }
        New-UDCard -Title "Querying $($Session:Server)" -Content {
            New-UDRow -Columns {
                New-UdColumn -Size 4 -Content {
                    New-UdMonitor -id "proc" -Title "CPU (% processor time)" -Type Line -DataPointHistory 20 -RefreshInterval 5 -ChartBackgroundColor '#80FF6B63' -ChartBorderColor '#FFFF6B63'  -Endpoint {
                        try {
                            Show-UDToast -Message "Should be $($Session:Server)"
                            Get-Counter -ComputerName $($Session:Server) -Counter '\Processor(_Total)\% Processor Time' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue | Out-UDMonitorData
                        }
                        catch {
                            Write-UDLog -Message "Exception $($Error[0].Exception)" -Level Error
                            50 | Out-UDMonitorData
                        }
                    }
                }
            }
        }
    }
} -Port 10000

Also, is there some way to reset the chart when I change servers, otherwise it’ll continue adding new data from the new server onto the old data from the old server?


#2

OK, so I found a fix… The question is tho, how come this works?

$sb = "Get-Counter -ComputerName $($Session:Server) -Counter '\Processor(_Total)\% Processor Time' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue | Out-UDMonitorData"
Invoke-Expression $sb

and this doesn’t?

Get-Counter -ComputerName $($Session:Server) -Counter '\Processor(_Total)\% Processor Time' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue | Out-UDMonitorData

Am I understanding something fundamentally wrong about how the binding of controls works, perhaps?
Thanks in advance for any enlightenment…!


#3

UDSelect adds double quotes to the value.

Try

$Session:Server = ( [string]$eventdata ).Trim( '"' )

Thanks,
Tim Curwick


#4

Oh man… I can’t believe I missed that. It was there in plain sight in my toast notifications as well!
Thanks Tim!

On the other question, any idea how I can get the graph to restart from new when I change the server?