private/core/Get-ZtAssessmentResults.ps1

function Get-ZtAssessmentResults {
    <#
    .SYNOPSIS
        Gets the results of all the Zero Trust Assessment tests
 
    .DESCRIPTION
        Gets the results of all the Zero Trust Assessment tests
        Ran as the last part of the assessment invocation.
 
        Can be used independently after the assessment command has completed to review the last results.
 
    .EXAMPLE
        PS C:\> Get-ZtAssessmentResults
 
        Gets the results of all the Zero Trust Assessment tests
    #>

    [CmdletBinding()]
    param ()

    #region Utility Functions
    function Get-ModuleLatestVersion {
        [CmdletBinding()]
        param (

        )

        if ((Get-Module -Name 'Microsoft.PowerShell.PSResourceGet') -or (Get-Command 'Find-PSResource' -ErrorAction Ignore)) {
            (Find-PSResource -Name ZeroTrustAssessment).Version -as [string]
        }
        elseif (Get-Command 'Find-Module' -ErrorAction SilentlyContinue) {
            (Find-Module -Name ZeroTrustAssessment).Version -as [string]
        }
        else {
            Write-Verbose -Message "Neither PowerShellGet nor PSResourceGet is available. Cannot determine latest module version."
            'Unknown'
        }
    }

    function Get-TestResultSummary {
        [CmdletBinding()]
        param (
            $TestResults,
            $PreviewEnabled
        )
        $summary = [PSCustomObject]@{
            IdentityPassed = @($TestResults).Where{ $_.TestPillar -eq 'Identity' -and $_.TestStatus -eq 'Passed' }.Count
            IdentityTotal  = @($TestResults).Where{ $_.TestPillar -eq 'Identity' -and $_.TestStatus -notin 'Skipped', 'Planned' }.Count
            DevicesPassed  = @($TestResults).Where{ $_.TestPillar -eq 'Devices' -and $_.TestStatus -eq 'Passed' }.Count
            DevicesTotal   = @($TestResults).Where{ $_.TestPillar -eq 'Devices' -and $_.TestStatus -notin 'Skipped', 'Planned' }.Count
        }

        if($PreviewEnabled){
            $summary | Add-Member -NotePropertyName 'NetworkPassed' -NotePropertyValue (@($TestResults).Where{ $_.TestPillar -eq 'Network' -and $_.TestStatus -eq 'Passed' }.Count)
            $summary | Add-Member -NotePropertyName 'NetworkTotal' -NotePropertyValue (@($TestResults).Where{ $_.TestPillar -eq 'Network' -and $_.TestStatus -notin 'Skipped', 'Planned' }.Count)
            $summary | Add-Member -NotePropertyName 'DataPassed' -NotePropertyValue (@($TestResults).Where{ $_.TestPillar -eq 'Data' -and $_.TestStatus -eq 'Passed' }.Count)
            $summary | Add-Member -NotePropertyName 'DataTotal' -NotePropertyValue (@($TestResults).Where{ $_.TestPillar -eq 'Data' -and $_.TestStatus -notin 'Skipped', 'Planned' }.Count)
        }

        return $summary
    }

    function Get-Organization {
        [CmdletBinding()]
        param (

        )
        $org = Invoke-ZtGraphRequest -RelativeUri 'organization'
        $defaultDomain = $org.verifiedDomains | Where-Object { $_.isDefault } | Select-Object -First 1
        return [PSCustomObject]@{
            TenantName = $org.displayName
            Domain     = $defaultDomain.name
        }
    }
    #endregion Utility Functions

    $mgContext = Get-MgContext
    $org = Get-Organization
    # Sort by risk then by status
    $tests = $script:__ZtSession.TestResultDetail.Value.values | Sort-Object -Property @{Expression = { $_.TestRisk } }, @{Expression = { $_.TestStatus } }

    $ztTestResults = [PSCustomObject][ordered]@{
        ExecutedAt        = Get-Date
        TenantId          = $mgContext.TenantId
        TenantName        = $org.TenantName
        Domain            = $org.Domain
        Account           = $mgContext.Account
        CurrentVersion    = $PSCmdlet.MyInvocation.MyCommand.Module.Version.ToString()
        LatestVersion     = Get-ModuleLatestVersion
        TestResultSummary = Get-TestResultSummary -TestResults $script:__ZtSession.TestResultDetail.Value.values -PreviewEnabled $script:__ZtSession.PreviewEnabled
        Tests             = @($tests) # Use @() to ensure it's an array
        TenantInfo        = Get-ZtTenantInfo
        EndOfJson         = "EndOfJson" # Always leave this as the last property. Used by the script to determine the end of the JSON
    }

    Write-PSFMessage $ztTestResults -Level Debug -Tag ZtAssessmentResults -Target $ztTestResults
    $ztTestResults
}