Inconsistent Page Content Initialization


#1

I have some code which is generating a weird issue across some of my pages. The main controller script runs a bit of code like this to source the various files:

Get-ChildItem -Path $pagedir -Filter *.ps1 -Recurse | ForEach-Object {
    . $_.FullName
}

One of my files runs a bit of code before the page objects in the .ps1:

$ADUsers = Invoke-Sqlcmd -Query “select * from ad_users” -ServerInstance $sqlinstance -Database $dbname
$UserCount = $ADUsers.Count
$EnabledUserCount = ($ADUsers | Where-Object -Property Enabled -eq -Value $True).Count
$DisabledUserCount = $UserCount - $EnabledUserCount
$LockedOutUsers = @($ADUsers | Where-Object -Property LockedOut -eq -Value $True)
$ADData = Invoke-Sqlcmd -Query “SELECT TOP 1 * FROM ad_summary ORDER BY date DESC” -ServerInstance $sqlinstance -Database $dbname
$Data = Invoke-Sqlcmd -Query “SELECT * FROM ad_summary” -ServerInstance $sqlinstance -Database $dbname
$Features = @();
Foreach ($D in $Data) {
$Features += [PSCustomObject]@{ “Date” = $D.date; “Users” = $D.total_users ; “EnabledUsers” = $D.total_users_enabled ; “Computers” = $D.total_computers ; “EnabledComputers” = $D.total_enabled_computers }
}

Later, within one of the page objects I have this code block:

New-UDColumn -Size 12 {
    New-UDTable -Title "AD Data" -Headers @("total_users", "total_users_enabled", "total_computers", "total_enabled_computers", "total_groups", "forest_functional_level") -Endpoint {
        $ADData.GetEnumerator() | Out-UDTableData -Property @("total_users", "total_users_enabled", "total_computers", "total_enabled_computers", "total_groups", "forest_functional_level")
    }
    New-UDChart -Title "AD Features Over Time" -Height 600px -Width 100% -Type Line -Endpoint {
        $Features | Out-UDChartData -LabelProperty Date -Dataset @(
            New-UDChartDataset -DataProperty "Users" -Label "Users" -BackgroundColor "#80962F23" -HoverBackgroundColor "#80962F23"
            New-UDChartDataset -DataProperty "EnabledUsers" -Label "EnabledUsers" -BackgroundColor "#800FF22F" -HoverBackgroundColor "#800FF22F"
            New-UDChartDataset -DataProperty "Computers" -Label "Computers" -BackgroundColor "#8014558C" -HoverBackgroundColor "#8014558C"
            New-UDChartDataset -DataProperty "EnabledComputers" -Label "EnabledComputers" -BackgroundColor "#803AE8CE" -HoverBackgroundColor "#803AE8CE"
        )
    }
}

The result is that the chart works but the table doesn’t:

However, as mentioned before, there is a controller script which sources everything then launches the dashboard. If I go to the file which contains the page and those variables, and run the file in my ise or powershell session and then go and launch the dashboard, it works!

It seems that $ADData is not initialized while $Data (which becomes $features) is. I have no idea why?


#2

I have my page script define just the desired contents of the endpoint, rather than the full page object.
The dashboard script defines the pages with endpoints that call the scripts. That way, the page definition scripts are not loaded until the page is called by a browser. And they are reloaded each time the page is called by a browser, so those scripts can be updated at any time without needing to restart the dashboard.

ForEach ( $Page in $SiteUnit )
    {
    $Auth = @{}
    If ( $Page.Role ) { $Auth.AuthorizedRole = $Page.Role }

    #  Page contents will be dynamicaly defined by referenced script at runtime
    New-UDPage -Id $Page.ScriptBase -URL $Page.URL -Endpoint ( [scriptblock]::Create( ". `"$ScriptFolder\Form.$($Page.ScriptBase).ps1`"" ) ) @Auth
    }

Thanks,
Tim Curwick


#3

That is some wicked powershell, will definitely play around with it. Thanks so much!