Private/Invoke-NSPConditionalAccessDashboardInventory.ps1

function Invoke-NSPConditionalAccessDashboardInventory {
    <#
    .SYNOPSIS
        Dashboard: takes and saves a fresh inventory when signed in to this tenant as the tool's app; says why not otherwise.
    .DESCRIPTION
        Read-only. Called when the dashboard opens, after anything that wrote to the tenant, after
        signing in, and between the passes of a guided task, so what is shown and planned next is
        the tenant as it is now. Returns $true when an inventory was saved.
 
        While it runs, the screen says so: -Screen clears it and shows an "Updating the
        inventory" banner (the main screen is redrawn afterwards); without it, a section rule
        keeps what is already on screen. The inventory's own progress bar shows each step.
    #>

    [CmdletBinding()]
    [OutputType([bool])]
    param(
        [Parameter(Mandatory)]$Session,
        [string]$Reason = 'Updating the inventory',
        [switch]$Screen
    )

    $Session.State = Get-NSPConditionalAccessDashboardState -TenantId $Session.TenantId -ConfigRoot $Session.ConfigRoot
    if (-not $Session.State.Connected) {
        Write-NSPConsoleLine " Not signed in to this tenant as the tool's app, so no fresh inventory was taken. Press C to sign in." -Role Warning
        return $false
    }
    if ($Screen) {
        Clear-NSPConsole
        Write-NSPConsoleHeader -Title $Reason -Subtitle ([string]$Session.State.TenantDomain)
    } else {
        Write-NSPConsoleRule -Title $Reason
    }
    Write-NSPConsoleSegment -Segment @(@(' Reading the tenant through Graph. ', 'Heading'), @('Read-only: nothing in the tenant changes. Usually under a minute; the progress bar shows each step.', 'Muted'))
    try {
        $inventory = Get-NSPConditionalAccessInventory -Save -ConfigRoot $Session.ConfigRoot
        Write-NSPConsoleLine " Inventory saved: $(@($inventory.Policies).Count) policies, $(@($inventory.NamedLocations).Count) named locations." -Role Success
        $Session.InventoryWanted = $false
        $Session.InventoryProblem = $null
        $Session.State = Get-NSPConditionalAccessDashboardState -TenantId $Session.TenantId -ConfigRoot $Session.ConfigRoot
        return $true
    } catch {
        Write-NSPConditionalAccessProgress -Activity Inventory -Completed
        Write-NSPConsoleLine " The inventory did not complete: $_" -Role Error
        $Session.InventoryProblem = "The last inventory did not complete: $_"
        $Session.InventoryWanted = $false
        return $false
    }
}