Get which tab is selected?

I’m trying to update a variable based on which UDTab is selected but I don’t seem to be able to get it to update dynamically

In the example below, the tabs render the service name correctly in the -Text but the UDAlert is blank and the $Session:Service variable gets changed to blank

Any thoughts?

Example:

New-UDTabs -RenderOnActive -Orientation vertical -id 'svctab' -Tabs {
   New-UDTab -Text 'All Services' -Dynamic -Content {
        $Session:Service="all"
        Sync-UDElement -Id 'issues'
   }       
   foreach ($service in $servicesListed) {
        New-UDTab -Text $service.Service -Dynamic -Content {
           New-UDAlert -Text $service.Service
           $Session:Service=$service.Service
       }
   }
}
Product: PowerShell Universal
Version: 2.9.2

Maybe because the New-UDTab is set to “Dynamic”, if you remove that, then this should work:

$servicesListed = Get-Service | select -first (Get-Random -Minimum 1 -Maximum 11)
New-UDTabs -RenderOnActive -Orientation vertical -id 'svctab' -Tabs {
	foreach ($service in $servicesListed) {
		New-UDTab -Text $service.Name  -Content {
			New-UDAlert -Text $service.Name
		}
	}
}

I did try getting the “Text” of the UDElement when they are dynamic, but had no luck with:

$servicesListed = Get-Service | select -first (Get-Random -Minimum 1 -Maximum 11)
New-UDTabs -RenderOnActive -Orientation vertical -id 'svctab' -Tabs {
	foreach ($service in $servicesListed) {
		New-UDTab -id "ServiceTabs$serviceName" -Text $service.Name -Dynamic -Content {
			$thisTabText = (Get-UDElement -Id "ServiceTabs$serviceName").Text
			New-UDAlert -Text $thisTabText
		}
	}
}

Someone will know the way :slight_smile:

Yes, foreach ($service in $servicesListed) is in the script scope.
But because you set New-UDTab to dynamic, it runs the New-UDAlert in a different scope that have no access to the $service variable.
You should use a $session variable to share the data between the main scope and the dynamic tab scopes.
I don’t know if tabs support the Get-UDElement command. You can verify the data received with (Get-UDElement -Id $id | ConvertTo-Json).

Thanks everyone! I’m getting much closer but could still use some help

Context: This is to display current service incidents as reported by Microsoft’s message center for certain users who don’t have access to the admin centers. The tab titles list the services with issues and inside each tab it lists the specific events for each service
image

These are in a UDGrid, with the ‘details’ UDDynamic in another UDGrid

The top one works fine (‘All Services’), all incidents show as expected and when the UDChip is clicked, the ‘details’ pane updates properly with the incident message.

The others now display the UDChips, but end up setting the $Session: variables to null

New-UDTabs -RenderOnActive -Orientation vertical -id 'svctab' -Tabs {
   
   New-UDTab -Text 'All Services' -Dynamic -Content {
    $existingIncidents | % {

      $curIncident = $_
      $subTitle = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($curIncident.Title))
      $curIncID = $curIncident.IncidentID
      
       New-UDChip -Label $subTitle -Icon (New-UDIcon -Icon 'ExclamationTriangle') -OnClick {
        $selectLatestMessage = "SELECT TOP(1) * FROM ServiceMessages WHERE IncidentID = '$($curIncident.IncidentID)' ORDER BY messageDateTime DESC"
        $latestMessage = Invoke-DbaQuery -SqlInstance $sqlServer -Query $selectLatestMessage
        $Session:inc = $curIncident
        $Session:message = $latestMessage
        Sync-UDElement -Id 'details'
       }
     }
   }
   
   foreach ($service in $servicesListed) {
    New-UDTab -Text $service.Service -Content {
     $existingIncidents | ? Service -eq $service.Service | % {

      $curIncident = $_
      $subTitle = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($curIncident.Title))
      $curIncID = $curIncident.IncidentID
      
       New-UDChip -Label $subTitle -Icon (New-UDIcon -Icon 'ExclamationTriangle') -OnClick {
        $selectLatestMessage = "SELECT TOP(1) * FROM ServiceMessages WHERE IncidentID = '$($curIncident.IncidentID)' ORDER BY messageDateTime DESC"
        $latestMessage = Invoke-DbaQuery -SqlInstance $sqlServer -Query $selectLatestMessage
        $Session:inc = $curIncident
        $Session:message = $latestMessage
        Sync-UDElement -Id 'details'
       }
     }
    }
   }
  }

I think $curIncident is not in the same scope, the OnClick script block is an endpoint.

Apparently all I needed to do was assign an Id to the UDChip. The below code functioned as expected (each chip updated the ‘details’ Element)

New-UDTabs -RenderOnActive -Orientation vertical -id 'svctab' -Tabs {
   
   New-UDTab -Text 'All Services' -Dynamic -Content {
    $existingIncidents | % {

      $curIncident = $_
      $subTitle = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($curIncident.Title))
      $curIncID = $curIncident.IncidentID
      
       New-UDChip -Label $subTitle -Id $curIncID -Icon (New-UDIcon -Icon 'ExclamationTriangle') -OnClick {
        $selectLatestMessage = "SELECT TOP(1) * FROM ServiceMessages WHERE IncidentID = '$($curIncident.IncidentID)' ORDER BY messageDateTime DESC"
        $latestMessage = Invoke-DbaQuery -SqlInstance $sqlServer -Query $selectLatestMessage
        $Session:inc = $curIncident
        $Session:message = $latestMessage
        Sync-UDElement -Id 'details'
       }
     }
   }
   
   foreach ($service in $servicesListed) {
    New-UDTab -Text $service.Service -Content {
     $existingIncidents | ? Service -eq $service.Service | % {

      $curIncident = $_
      $subTitle = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($curIncident.Title))
      $curIncID = $curIncident.IncidentID

      New-UDChip -Label $subTitle -Id $curIncID -Icon (New-UDIcon -Icon 'ExclamationTriangle') -OnClick {
        $selectLatestMessage = "SELECT TOP(1) * FROM ServiceMessages WHERE IncidentID = '$($curIncident.IncidentID)' ORDER BY messageDateTime DESC"
        $latestMessage = Invoke-DbaQuery -SqlInstance $sqlServer -Query $selectLatestMessage
        $Session:inc = $curIncident
        $Session:message = $latestMessage
        Sync-UDElement -Id 'details'
       } <##>
     }
    }
   }
  }
1 Like

sounds like a really neat idea.

Mind sharing the code that grabs the messages?

Thanks @PorreKaj!

I’m essentially hitting the microsoft graph api at graph.microsoft.com/beta/admin/serviceAnnouncement/issues every few minutes and storing the incidents in a database - I use the latest lastModifiedDateTime attribute in a ?filter= parameter as well

List issues - Microsoft Graph v1.0 | Microsoft Docs

Anything I’m unsure of the formatting (the message content and title, so far) I just convert to base64 before inserting to the dbase and convert back when displaying in the dashboard

2 Likes