Pages under Dashboard doesn't see update variables from endpoint in main start file

Hello:

I have problem with passing variable from main file to Page file.
Under main file I have some endpoint that should be refreshed.

Under Page I want to use above endpoint but it looks like that variable $Cache:Data_CountLogons_Input is empty.

Everything works fine under Powershell, but under IIS - it doesn’t work.

  1. Dashboard file

#Generate pages from files
$PagesPath = Join-Path $PSScriptRoot “Pages”
$Footer = . (Join-Path $PagesPath “footer.ps1”)

$Pages = @()
$Pages += . (Join-Path $PagesPath “home.ps1”)

Get-ChildItem -Path $PagesPath -Exclude “home.ps1”, “footer.ps1” | ForEach-Object {
$Pages += . $_.FullName
}

#Refresh data

#Schedule
$Schedule = New-UDEndpointSchedule -Every 60 -Second

#Endpoint
$Endpoint = New-UDEndpoint -Schedule $Schedule -Endpoint {
$Query =
@"
SELECT HostName, COUNT(*) AS Logons FROM [RSDSCReporting].[dbo].[RDPLogons] WHERE (LogonDate >= DATEADD(day,-7, GETDATE())) GROUP BY HostName
"@
$Cache:Data_CountLogons_Input = Get-ODBCData -Query $Query -ConnectionString $env:DSC_CONNECTIONSTRING
}

Initialize the Dashboard

$Theme = Get-UDTheme “Default”
$Initialization = New-UDEndpointInitialization -Module ‘DscDashboard’ -Variable ‘$Cache:Data_CountLogons_Input’

Start-UDDashboard -Endpoint $Endpoint -Dashboard (

New-UDDashboard -Theme $Theme -Title "Reform School DSC Dashboard" -Pages $Pages -EndpointInitialization $Initialization -Footer $Footer -NavbarLinks $NavBarLinks

) -AutoReload -Port 4242 -Wait #Wait is needed for hosting

  1. One of Page file

#Create Object
$Cache:Data_CountLogons = New-Object System.Collections.Generic.List[System.Object]

#Testing
Set-Content -Path c:\data.txt -Value $Cache:Data_CountLogons_Input

#Pass data from SQL to object
$Cache:Data_CountLogons_Input |

ForEach-Object {

$Data_Object = New-Object PSObject
$Data_Object = [PSCustomObject]@{
   Hostname = $_.Hostname
    Logons   = $_.Logons
}
$Cache:Data_CountLogons.Add($Data_Object)

}

#Testing
Set-Content -Path c:\data2.txt -Value $Cache:Data_CountLogons

New-UDPage -Name “Logins Counter” -Icon bar_chart -Content {

New-UDCard -Content {

    New-UDGrid -Title "Count of last 7 days RDP Logins for given server" -Headers @(
    
    "Hostname", "Logons"

    ) -Properties @(

    "Hostname", "Logons"
        
    ) -Endpoint {
        #Import-Module 'UniversalDashboard.Community' -MinimumVersion 2.0.1 -ErrorAction 'Stop' -Force
        #Import-Module 'DscDashboard' -Force

        $Cache:Data_CountLogons | Out-UDGridData

    } -PageSize 25 -DefaultSortColumn StartTime -DefaultSortDescending -DateTimeFormat "LLLLL" -AutoRefresh -RefreshInterval (10*60)  #-ServerSideProcessing

} # Card

} # Page

#>

Hi @mk123456 and welcome to the Ironman forums!

Two issues might be causing this, firstly on your page you nullify your variable on load. Remove the following line:

$Cache:Data_CountLogons = New-Object System.Collections.Generic.List[System.Object]

Secondly, nonspecified scriptblocks are defined as static content, IE it’s only executed once at the launch of your dashboards and not on each load, unless the page’s is using -endpoint instead of -content. See @psDevUK blog for this:

TL;DR:
Move the

$Cache:Data_CountLogons_Input |

ForEach-Object {

$Data_Object = New-Object PSObject
$Data_Object = [PSCustomObject]@{
   Hostname = $_.Hostname
    Logons   = $_.Logons
}
$Cache:Data_CountLogons.Add($Data_Object)
}

Inside the grid endpoint and throw the “out-udgriddata” on the end, and it should do the trick.

TL;DR move your

1 Like

Thank you very much for your time :slight_smile: Above line has different name then “variable with data” - there are two simillar variables - one is with “Input” suffix. Sorry for confusion

I will check you second observation - Thanks again! I hope it will help

Thank you again. It was my issue :slight_smile: Solved

1 Like