Private/Get-GraphReportCsv.ps1

# Get-GraphReportCsv.ps1
# Downloads a Graph reporting API endpoint to a temp CSV and returns the parsed data.
# Part of the M365-QuickAssess module -- not exported.

function Get-GraphReportCsv
{
    param
    (
        [Parameter(Mandatory)]
        [string]$Uri
    )

    $tempFile = $null

    try
    {
        $tempFile = Join-Path $env:TEMP ( "GraphReport_{0}.csv" -f ( [guid]::NewGuid().ToString() ) )

        Invoke-MgGraphRequest -Method GET -Uri $Uri -OutputFilePath $tempFile | Out-Null

        if ( -not ( Test-Path $tempFile ) )
        {
            Write-Log "Graph report download produced no file: $Uri" "WARN"
            return $null
        }

        $data = Import-Csv -Path $tempFile

        return $data
    }
    catch
    {
        Write-Log "Graph report failed: $Uri" "WARN"
        Write-Log $_.Exception.Message "WARN"
        return $null
    }
    finally
    {
        # -------------------------------------------------------------------
        # Always clean up the temp file regardless of success or failure
        # -------------------------------------------------------------------
        if ( $tempFile -and ( Test-Path $tempFile ) )
        {
            Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
        }
    }
}