Working with variables created from User input

I have a simple dashboard that takes a User ID via a New-UDInputAction, creates a variable $UserID, then fetches a list of AD groups that ID is a member of and displays them in a New-UDTable. That part works fine however, I want to be able to retain the value of $UserID to use in another section of the dashboard and have been unable to do so.

If I use a Scope, it either fails to work at all (pretty sure that’s my fault as I`m not 100% sure where in the code that should be defined) or retains it indefinitely so it is not overwritten with a new value if a new User ID is queried.

Code is below and yeah, it’s not pretty but it works up to a point (like me I guess :wink:)

I have Toasts set to trigger as progress indicators and the “EMail” button -OnClick should trigger a toast that also contains the $UserID variable.

Is what I`m attempting to do possible ?

Cheers,

Import-Module UniversalDashboard
Import-Module ActiveDirectory
Import-Module UniversalDashboard.Style

Get-UDDashboard | Stop-UDDashboard

$Dashboard = New-UDDashboard -Title "Orange Styled Page with Input and 4 buttons - Scratch copy" -NavBarcolor '#E98300' -Content {

$TopLevelServer= "DC.Contoso.com"
$ResourceDomainServer= "DC.Contoso.com"
$ResourceContextServer = "gb.Contoso.com"
 
New-UDRow -Columns {   
             
    New-UDColumn -Size 5 -Content {

                New-UDStyle -Style '
            .ud-input .btn {
                background-color:#E98300;
             }
               ' -Content {

   New-UDInput -Title "Active Directory Group Membership" -Endpoint { 
    Param(
    [Parameter(Mandatory)]
    [string]$UserID)

New-UDInputAction -Content {
                        
  New-UDTable -Title "Active Directory Group Membership for $UserID " -Headers @("Active Directory Group Name") -Endpoint {     

         Show-UDToast -Title "Testing $UserID Variable" -Message "Fetching AD Group details for $UserID - Please Wait" -Theme dark -Duration 10000 -Balloon -Position center
                                                                                                                                     
         Get-ADPrincipalGroupMembership -Identity $UserID -Server $TopLevelServer | Select-Object SamAccountName | Sort-Object SamAccountName | Out-UDTableData -Property @("SamAccountName")
                           
         Show-UDToast -Title "Testing $UserID Variable" -Message "Still going...." -Theme dark -Duration 10000 -Balloon -Position center
          
         Get-ADPrincipalGroupMembership -Identity $UserID -Server $TopLevelServer  -ResourceContextServer $ResourceContextServer | Select-Object SamAccountName | Sort-Object SamAccountName | Out-UDTableData -Property @("SamAccountName")                                                     

     }
    }
   }
    }
     }   

New-UDColumn -Size 3 -Content{
 
        New-UDButton -Id 'Orange Email Button' -BackgroundColor "#E98300" -Text "1. EMail" -Style @{"margin-top"="8px";"Width"="100%";"Height"="50px"} -OnClick {
     Show-UDToast -Title "Testing Email Button $UserID Variable" -Message "$UserID" -Theme dark -Duration 10000 -Balloon -Position center
     }

    New-UDButton -Id 'Button 2' -BackgroundColor "#FF0000" -Text "2." -Style @{"margin-top"="8px";"Width"="100%";"Height"="50px"} # -OnClick {
    New-UDButton -Id 'Button 3' -BackgroundColor "#000000" -Text "3." -Style @{"margin-top"="8px";"Width"="100%";"Height"="50px"} # -OnClick {
    New-UDButton -Id 'Button 4' -BackgroundColor "#006600" -Text "4." -Style @{"margin-top"="8px";"Width"="100%";"Height"="50px"} # -OnClick {
 }
  }      
}  

         Start-UDDashboard -Dashboard $Dashboard -Port 10001 # -AutoReload

Replying to myself - it’s been one of those weeks

For anyone who may stumble across this, I added the $Cache:UserID = $UserID to the
New-UDInput Param block

  New-UDInput -Title "Active Directory Group Membership" -Endpoint { 
    Param(
    [Parameter(Mandatory)]
    [string]$UserID)
    $Cache:UserID = $UserID

Then changed any references of $UserID to $Cache:UserID in the New-UDTable and the New-UDButton sections:

  New-UDTable -Title "Active Directory Group Membership for $Cache:UserID " -Headers @("Active Directory Group Name") -Endpoint {     
                         
         Show-UDToast -Title "Testing $Cache:UserID Variable" -Message "Fetching AD Group details for $Cache:UserID - Please Wait" -Theme dark -Duration 10000 -Balloon -Position center
                                                                                                                                     
         Get-ADPrincipalGroupMembership -Identity $Cache:UserID -Server $TopLevelServer | Select-Object SamAccountName | Sort-Object SamAccountName | Out-UDTableData -Property @("SamAccountName")
                           
         Show-UDToast -Title "Testing $Cache:UserID Variable" -Message "Still going...." -Theme dark -Duration 10000 -Balloon -Position center
          
         Get-ADPrincipalGroupMembership -Identity $Cache:UserID -Server $TopLevelServer  -ResourceContextServer $ResourceContextServer | Select-Object SamAccountName | Sort-Object SamAccountName | Out-UDTableData -Property @("SamAccountName")         
        New-UDButton -Id 'Orange Email Button' -BackgroundColor "#E98300" -Text "1. EMail" -Style @{"margin-top"="8px";"Width"="100%";"Height"="50px"} -OnClick {
     Show-UDToast -Title "Testing Email Button $Cache:UserID Variable" -Message "$Cache:UserID" -Theme dark -Duration 10000 -Balloon -Position center

That seems to have fixed it. I could have sworn that I`d tried that before but I guess not :man_shrugging:

$Cache:UserID will be available to all endpoints of all users of your dashboard. Everyone will get the last value set.
$Session:UserID would be available to all endpoints on a per user basis.
https://docs.universaldashboard.io/endpoints/custom-variable-scopes

gav

@OpsEng That’s a very good point, thank you.

While I don’t expect this dashboard to be heavily used in its current form, I had overlooked the possibility of it having to serve multiple concurrent users which could potentially happen, especially if more features are added.

I`ll change $Cache:UserID to $Session:UserID

Cheers