$User variable not accessible through entire dashboard script

Trying to access $User prior to loading the pages so I can share the information through multiple pages without the code being pasted multiple times. It complains the $User is empty. However, the card loads with the user just fine.

Am I doing something wrong? I could have swore I had it working earlier.

$userObj = Get-AdUser $User -Properties Location,Position

$Pages += New-UDPage -Name "Home" -Url '/Home' -Content {
    New-UDGridLayout -Content {
          New-UDImage -Url '/Images/logo.png' -id Image
        
        New-UDCard -Title "Welcome $User" -id 'welcomeMsg' -Content { 
Product: PowerShell Universal
Version: 3.0.5

If you are trying to access a variable in multiple different scripts I would use the built in Variable tool in PSU. In V 3.0.0 its under platform/variables. This also allows you to update the variable from within the script. I tend to use an api to update the data though.

Hey @StevenM

I am trying to access the PSU Built In Variables.
$User should be populated with the user logged in, but it seems it is not accessible outside of $Pages

$userObj = Get-AdUser $User -Properties Location,Position
$Pages = @()
$Pages += New-UDPage -Name "Home for $User" -Url '/Home' -Content {
    New-UDCard -Title "Welcome $User - Your Location: $userObj.Location"
}
New-UDDashboard -Pages $Pages -Title 'Test' -Theme $Theme

$User works inside the Card, but not the name of the page.
Also the userObj which is set at the top of the dashboard script is not accessible inside the pages. This may be intended, but wanted to clarify if I need to duplicate the get-ADUser call through the pages.

The $User variable won’t be available outside of the page scope.

# Won't work here
$Pages = @()
$Pages += New-UDPage -Name "Home for $User" -Url '/Home' -Content {
   # Will work here
}
New-UDDashboard -Pages $Pages -Title 'Test' -Theme $Theme

Same for dashboard content.

# Won't work here
$Pages = @()
New-UDDashboard -Pages $Pages -Title 'Test' -Content {
# Will work here
}

The reason is that the global scope is called once when the dashboard is started and not called again when the user loads the page. The content of the page and dashboard is called again when the user views the page.

2 Likes

Thank you @adam for the information!
Is there any way to achieve sharing the Active Directory Object through the pages without calling AD Object every page?

You could solve it using $session variables, see Universal Dashboard Loading Dialogs for example.

use a global cache scope or save the object to XML and reload, export-clixml import-clixml