I am attempting to convert the following code into a function:
New-UDTableColumn -Property CPU -Title "CPU (%)" -Render {
New-UDElement -tag div -Content {
if ([string]::IsNullOrEmpty($EventData.CPU) -eq 'True' ) {
New-UdTypography -Text "No Data"
}
elseif ($EventData.CPU -gt 90) {
New-UDTooltip -TooltipContent { "CPU is currently at: " + [String]$EventData.CPU +"%!" } -Content {
New-UDIcon -Icon exclamationtriangle -Size 2x -Color red
}
if((Get-UDElement -Id 'AlertsSwitch').checked -eq 'True'){
$Body = [String]$EventData.ServerName + " CPU is currently at: " + [String]$EventData.CPU +"%!"
Show-UDToast -Message $Body -BackgroundColor red -MessageColor white -Duration 30000
}
}
elseif ($EventData.CPU -gt 70) {
New-UDIcon -Icon exclamationtriangle -Size 2x -Color orange
} else {
New-UDIcon -Icon checkcircle -Size 2x -Color green
}
}
I have many different columns similar to this, which is why I am trying to turn it into a function so I can make the code of my dashboard easier to understand and maintain. Here is what I pass in to call my custom function:
New-MainTableColumn -ColumnProperty 'CPU' -ColumnTitle 'CPU(%)' -ThresholdOne 40 -ThresholdTwo 50
…And here is the function code itself that dynamically creates the column:
function New-MainTableColumn {
param($ColumnProperty, $ColumnTitle, $ThresholdOne, $ThresholdTwo)
$Cache:ColumnProperty = $ColumnProperty
$Cache:ThresholdOne = $ThresholdOne
$Cache:ThresholdTwo = $ThresholdTwo
New-UDTableColumn -Property "$ColumnProperty" -Title "$ColumnTitle" -Render {
New-UDElement -tag div -Content {
$ColumnProperty = $Cache:ColumnProperty
$ThresholdOne = $Cache:ThresholdOne
$ThresoldTwo = $Cache:ThresholdTwo
#Display No Data warning on dashboard
if ([string]::IsNullOrEmpty($EventData.$ColumnProperty) -eq 'True' ) {
New-UdTypography -Text "No Data"
}
elseif ($EventData.$ColumnProperty -gt $ThresoldTwo) {
New-UDTooltip -TooltipContent { "$ColumnProperty is currently at: " + [String]$EventData.$ColumnProperty } -Content {
New-UDIcon -Icon exclamationtriangle -Size 2x -Color red
}
if((Get-UDElement -Id 'AlertsSwitch').checked -eq 'True'){
$Body = [String]$EventData.ServerName + " $ColumnProperty is currently at: " + [String]$EventData.$ColumnProperty
Show-UDToast -Message $Body -BackgroundColor red -MessageColor white -Duration 3000
}
}
elseif ($EventData.$ColumnProperty -gt $ThresholdOne) {
New-UDTooltip -TooltipContent { " $ColumnProperty is currently at: " + [String]$EventData.$ColumnProperty } -Content {
New-UDIcon -Icon exclamationtriangle -Size 2x -Color Orange
}
} else {
New-UDTooltip -TooltipContent { " $ColumnProperty is currently at: " + [String]$EventData.$ColumnProperty } -Content {
New-UDIcon -Icon checkcircle -Size 2x -Color green
}
}
}
}
}
My issue is that as soon as I add an additional function call like so:
New-MainTableColumn -ColumnProperty 'CPU' -ColumnTitle 'CPU(%)' -ThresholdOne 40 -ThresholdTwo 50
New-MainTableColumn -ColumnProperty 'FailedJobs_Count' -ColumnTitle 'Failed Jobs' -ThresholdOne 40 -ThresholdTwo 50
…It seems that the table actually renders both column, but the values themselves both show data for the last function call parameters passed in:
So obviously it seems that whatever the last function call is… is actually what is cached and everything before that is overwritten. I’ve tinkered around with ideas about setting variables dynamically, but I am still falling short on how to accomplish this or if it is even a good idea to do so.