Is there a way to read the Name property of a UDPage object from within a nested UDGrid Endpoint scriptblock?
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
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.
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.
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
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?
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
}
}
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!