Referencing UDPage properties

Is there a way to read the Name property of a UDPage object from within a nested UDGrid Endpoint scriptblock?

1 Like

Hey @skatterbrainz are you using the -id parameter on your UD elements you are placing on the page? If so you should be able to do it via that.

I havenā€™t been, but I will now. But how would it work in the example below? What would I use in the $___ location to reference the ā€œtest123ā€ value?

New-UDPage -Name "test123" -Id "page1" -Content {
    New-UDParagraph -Text "PageName = $___"
}

So the real problem Iā€™m having is understanding scope of variables within nested UD blocks of code. It seems like sometimes setting a variable above New-UDPage in a script file, isnā€™t accessible inside the New-UDPage nested objects, so I have to move the variable assignments inside the same -Endpoint {} scriptblock in order to be visible. Am I doing something wrong?

Yeah UD does have some different levels of scoping. I mean have you looked into the $session variable? I tend to put all my $cache variables in there own page and then I know I can use any of those $cache variables anywhere on my dashboard in an endpoint script block. If you not doing it already put all pages in their own page. Makes troubleshooting your dashboards a lot easier too. I have got an example on github. I will try to find time to put an example that answers your above question

1 Like

I donā€™t see any explicit mention of $Cache or $Session on the API docs site, but I may be looking in the wrong place.

However, since Iā€™m on the subject, Iā€™d like to set some ā€œglobalā€ variables in the main .psm1 loading function (e.g. Start-UDMyDashboard). But Iā€™m having mixed results.

Hi @skatterbrainz

The $cache and $session is documented in
https://docs.universaldashboard.io/endpoints/custom-variable-scopes

Anything you store in $cache is avaliable through all your endpoints. $session is per user.

1 Like

Top man @BoSen29 just to add one last tiny bit of information to download to your brain is the next page:- https://docs.universaldashboard.io/endpoints/endpoint-initialization
As you mentioned above you wanted global variablesā€¦so you achieve this through using the endpoint-initialization. I got a page here on github using this:-
https://github.com/psDevUK/psUniversalDashboard/blob/master/FleetManagementGitHub.ps1

$Init = New-UDEndpointInitialization -Variable ā€œRootā€ -Function ā€˜Invoke-Sqlcmd2ā€™

So this allows me to use $Root throughout my dashboard, and call the function Invoke-Sqlcmd2 throughout my dashboard, without having to paste the whole function inside an endpoint, just calling itā€¦same with the $Root variable which I stole from the demonstrations Adam has posted on the official universal dashboard github. Hopefully armed with this information and the special scopoing variables you will be at ninja level in no time :1st_place_medal:

2 Likes

Thatā€™s really cool! Iā€™m using dbatools, so I use Invoke-DbaQuery and other functions quite a lot. Iā€™m passing the same -SqlInstance and -Database params across a lot of files, and the only thing that differs is the -File param (since Iā€™m using Get-Content to read .sql files for isolation).
Iā€™d like to use the UDPage or UDGrid -Id parameter value to control the file name arguments (set once and read multiple times). For example, the $pagename (-Id param) for the $qfile reference. Iā€™d like to clean that up more. Maybe I should use the New-UDEndpointInitialization method to define the other two variables, but how could I read the ID property of the Page or Grid to use inside as a value?

New-UDPage -Name "PageName" -Id 'pagename' -Content {
	New-UDGrid -Title "Grid Title" -Endpoint {
        $SiteHost = $Cache:ConnectionInfo.Server
        $SiteCode = $Cache:ConnectionInfo.SiteCode
        $BasePath = $Cache:ConnectionInfo.BasePath
        $qfile    = Join-Path $BasePath "queries\$pagename.sql"
        Invoke-DbaQuery -SqlInstance $SiteHost -Database "CM_$SiteCode" -File $qfile | Out-UDGridData
   }
}

Hi @skatterbrainz,
As far as i know, there is no way of reading the parentā€™s ID without some fancy clientside javascript reporting it back to a rest-api. Would however not recommend this path.

Unless @adam has any insight?

1 Like

You could do this with dynamic pages.

New-UDPage -Name "PageName" -Url '/:pageName' -Endpoint {
        param($PageName)

	New-UDGrid -Title "Grid Title" -Endpoint {
        $SiteHost = $Cache:ConnectionInfo.Server
        $SiteCode = $Cache:ConnectionInfo.SiteCode
        $BasePath = $Cache:ConnectionInfo.BasePath
        $qfile    = Join-Path $BasePath "queries\$pagename.sql"
        Invoke-DbaQuery -SqlInstance $SiteHost -Database "CM_$SiteCode" -File $qfile | Out-UDGridData
   }
}
3 Likes

omg - it still wasnā€™t clicking in my head until I looked further into the github example you provided. And holy cow that was it! Thank you! :slight_smile:

1 Like