# .TITLE Endpoint Analytics Report .SYNOPSIS Generate comprehensive Endpoint Analytics reports from Microsoft Intune including startup performance, application reliability, battery health, and work from anywhere metrics. .DESCRIPTION This script connects to Microsoft Graph API (beta) and retrieves Endpoint Analytics data from Intune. It collects metrics across multiple categories including device startup performance, application reliability, battery health, work from anywhere readiness, and overall device scores. Results are exported to CSV and HTML formats for analysis and reporting purposes. .TAGS Monitoring,Reporting .MINROLE Intune Administrator .PERMISSIONS DeviceManagementManagedDevices.Read.All .AUTHOR Ugur Koc .VERSION 1.4 .CHANGELOG 1.4 - Added Azure Automation contract validation, portal-safe boolean parameters, beta Graph endpoints, and terminating paging errors 1.3 - Azure Automation now records script progress, outcomes, and summaries in job history 1.2 - The ShowProgress switch now drives Write-Progress during metric collection; analytics queries select only the fields the report consumes; output directory is created automatically before exports; pagination helper keeps single-item results as arrays 1.1 - Local runs now use MgGraphCommunity for WAM-free interactive sign-in (auto-installed if missing); work from anywhere metrics now use the allDevices metricDevices endpoint 1.0 - Initial release .LASTUPDATE 2026-07-30 .EXAMPLE .\get-endpoint-analytics-report.ps1 Generates a complete Endpoint Analytics report with all metrics .EXAMPLE .\get-endpoint-analytics-report.ps1 -OutputPath "C:\Reports" -IncludeStartupPerformance "true" Generates report with only startup performance metrics .EXAMPLE .\get-endpoint-analytics-report.ps1 -IncludeAll "true" -ExportJson "true" Generates report with all metrics and exports to both CSV and JSON formats .NOTES - Requires Microsoft.Graph.Authentication module: Install-Module Microsoft.Graph.Authentication - Uses Microsoft Graph Beta API endpoints - Some features require Intune Advanced Analytics license - Battery Health metrics require Windows 10/11 devices - Endpoint Analytics must be enabled in Intune - Documentation: https://learn.microsoft.com/en-us/intune/analytics/overview - Local interactive sign-in uses the MgGraphCommunity module to avoid the Graph SDK's mandatory WAM broker on Windows #> [CmdletBinding()] param( [Parameter(Mandatory = $false, HelpMessage = "Directory path to save reports")] [ValidateNotNullOrEmpty()] [string]$OutputPath = ".", [Parameter(Mandatory = $false, HelpMessage = "Include startup performance metrics")] [ValidateSet("true", "false", "1", "0", '$true', '$false')] [string]$IncludeStartupPerformance, [Parameter(Mandatory = $false, HelpMessage = "Include application reliability metrics")] [ValidateSet("true", "false", "1", "0", '$true', '$false')] [string]$IncludeAppReliability, [Parameter(Mandatory = $false, HelpMessage = "Include battery health metrics")] [ValidateSet("true", "false", "1", "0", '$true', '$false')] [string]$IncludeBatteryHealth, [Parameter(Mandatory = $false, HelpMessage = "Include work from anywhere metrics")] [ValidateSet("true", "false", "1", "0", '$true', '$false')] [string]$IncludeWorkFromAnywhere, [Parameter(Mandatory = $false, HelpMessage = "Include all available metrics")] [ValidateSet("true", "false", "1", "0", '$true', '$false')] [string]$IncludeAll, [Parameter(Mandatory = $false, HelpMessage = "Export results in JSON format as well")] [ValidateSet("true", "false", "1", "0", '$true', '$false')] [string]$ExportJson, [Parameter(Mandatory = $false, HelpMessage = "Show progress during processing")] [ValidateSet("true", "false", "1", "0", '$true', '$false')] [string]$ShowProgress, [Parameter(Mandatory = $false, HelpMessage = "Force module installation without prompting")] [ValidateSet("true", "false", "1", "0", '$true', '$false')] [string]$ForceModuleInstall ) # Normalize the local module-install override for Azure Automation parameter binding. $forceModuleInstallRaw = [string]$ForceModuleInstall Remove-Variable -Name ForceModuleInstall if ([string]::IsNullOrWhiteSpace($forceModuleInstallRaw)) { $ForceModuleInstall = $false } elseif ($forceModuleInstallRaw.Trim().ToLowerInvariant() -in @("true", "1", '$true')) { $ForceModuleInstall = $true } elseif ($forceModuleInstallRaw.Trim().ToLowerInvariant() -in @("false", "0", '$false')) { $ForceModuleInstall = $false } else { throw "Parameter 'ForceModuleInstall' accepts only true, false, 1, 0, $true, or $false." } # Azure Automation supplies portal parameter values as strings. Normalize the # public boolean parameters once so local and runbook execution use real booleans. foreach ($runbookBooleanParameter in @('IncludeStartupPerformance', 'IncludeAppReliability', 'IncludeBatteryHealth', 'IncludeWorkFromAnywhere', 'IncludeAll', 'ExportJson', 'ShowProgress')) { $runbookBooleanRaw = [string](Get-Variable -Name $runbookBooleanParameter -ValueOnly) Remove-Variable -Name $runbookBooleanParameter if ([string]::IsNullOrWhiteSpace($runbookBooleanRaw)) { Set-Variable -Name $runbookBooleanParameter -Value $false continue } switch ($runbookBooleanRaw.Trim().ToLowerInvariant()) { { $_ -in @("true", "1", '$true') } { Set-Variable -Name $runbookBooleanParameter -Value $true } { $_ -in @("false", "0", '$false') } { Set-Variable -Name $runbookBooleanParameter -Value $false } default { throw "Parameter '$runbookBooleanParameter' accepts only true, false, 1, 0, $true, or $false." } } } # If no specific category is selected, include all by default if (-not ($IncludeStartupPerformance -or $IncludeAppReliability -or $IncludeBatteryHealth -or $IncludeWorkFromAnywhere -or $IncludeAll)) { $IncludeAll = $true } # If IncludeAll is set, enable all categories if ($IncludeAll) { $IncludeStartupPerformance = $true $IncludeAppReliability = $true $IncludeBatteryHealth = $true $IncludeWorkFromAnywhere = $true } # ============================================================================ # ENVIRONMENT DETECTION AND SETUP # ============================================================================ function Initialize-RequiredModule { param( [string[]]$ModuleNames, [bool]$IsAutomationEnvironment, [bool]$ForceInstall = $false ) foreach ($ModuleName in $ModuleNames) { Write-Verbose "Checking module: $ModuleName" $module = Get-Module -ListAvailable -Name $ModuleName | Select-Object -First 1 if (-not $module) { if ($IsAutomationEnvironment) { $errorMessage = @" Module '$ModuleName' is not available in this Azure Automation Account. To resolve this issue: 1. Go to Azure Portal 2. Navigate to your Automation Account 3. Go to 'Modules' > 'Browse Gallery' 4. Search for '$ModuleName' 5. Click 'Import' and wait for installation to complete "@ throw $errorMessage } else { Write-Information "Module '$ModuleName' not found. Attempting to install..." -InformationAction Continue if (-not $ForceInstall) { $response = Read-Host "Install module '$ModuleName'? (Y/N)" if ($response -notmatch '^[Yy]') { throw "Module '$ModuleName' is required but installation was declined." } } try { $isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator") $scope = if ($isAdmin) { "AllUsers" } else { "CurrentUser" } Write-Information "Installing '$ModuleName' in scope '$scope'..." -InformationAction Continue Install-Module -Name $ModuleName -Scope $scope -Force -AllowClobber -Repository PSGallery Write-Information "✓ Successfully installed '$ModuleName'" -InformationAction Continue } catch { throw "Failed to install module '$ModuleName': $($_.Exception.Message)" } } } try { Write-Verbose "Importing module: $ModuleName" Import-Module -Name $ModuleName -Force -ErrorAction Stop Write-Verbose "✓ Successfully imported '$ModuleName'" } catch { throw "Failed to import module '$ModuleName': $($_.Exception.Message)" } } } # Detect execution environment if ($PSPrivateMetadata.JobId.Guid) { Write-Output "Running inside Azure Automation Runbook" $IsAzureAutomation = $true } else { Write-Output "Running locally in IDE or terminal" $IsAzureAutomation = $false } # Initialize required modules $RequiredModules = @( "Microsoft.Graph.Authentication" ) # MgGraphCommunity gives WAM-free interactive sign-in for local runs if (-not $IsAzureAutomation) { $RequiredModules += "MgGraphCommunity" } try { Initialize-RequiredModule -ModuleNames $RequiredModules -IsAutomationEnvironment $IsAzureAutomation -ForceInstall $ForceModuleInstall Write-Verbose "✓ All required modules are available" } catch { Write-Error "Module initialization failed: $_" exit 1 } # ============================================================================ # AUTHENTICATION # ============================================================================ try { if ($IsAzureAutomation) { Write-Output "Connecting to Microsoft Graph using Managed Identity..." Connect-MgGraph -Identity -NoWelcome -ErrorAction Stop Write-Output "✓ Successfully connected to Microsoft Graph using Managed Identity" } else { Write-Output "Connecting to Microsoft Graph with interactive authentication..." $Scopes = @( "DeviceManagementManagedDevices.Read.All" ) Connect-MgGraphCommunity -Scopes $Scopes -NoWelcome -ErrorAction Stop Write-Output "✓ Successfully connected to Microsoft Graph" } } catch { Write-Error "Failed to connect to Microsoft Graph: $($_.Exception.Message)" exit 1 } # ============================================================================ # HELPER FUNCTIONS # ============================================================================ function Get-MgGraphAllPage { param( [string]$Uri, [int]$DelayMs = 100 ) $allResults = @() $nextLink = $Uri $requestCount = 0 do { try { if ($requestCount -gt 0) { Start-Sleep -Milliseconds $DelayMs } $response = Invoke-MgGraphRequest -Uri $nextLink -Method GET $requestCount++ if ($null -ne $response.value) { $allResults += $response.value } else { $allResults += $response } $nextLink = $response.'@odata.nextLink' if ($requestCount % 10 -eq 0) { Write-Verbose "Processed $requestCount requests..." } } catch { if ($_.Exception.Message -like "*429*" -or $_.Exception.Message -like "*throttled*") { Write-Information "`nRate limit hit, waiting 60 seconds..." -InformationAction Continue Start-Sleep -Seconds 60 continue } throw "Error fetching data from $nextLink : $($_.Exception.Message)" } } while ($nextLink) # Comma prevents unrolling so single-element results stay arrays return , $allResults } function Get-SafeAverage { param( [Parameter(Mandatory = $true)] [AllowEmptyCollection()] [object[]]$InputObject, [Parameter(Mandatory = $true)] [string]$Property, [double]$Divisor = 1 ) $numericValues = @( foreach ($item in $InputObject) { $propertyValue = $item.PSObject.Properties[$Property].Value if ($null -ne $propertyValue -and $propertyValue -is [System.IConvertible]) { try { [double]$propertyValue } catch { continue } } } ) if ($numericValues.Count -eq 0) { return 0 } return [math]::Round(($numericValues | Measure-Object -Average).Average / $Divisor, 1) } # ============================================================================ # MAIN SCRIPT LOGIC # ============================================================================ try { Write-Output "Starting Endpoint Analytics report generation..." $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss" $allMetrics = @{} # Device Scores - Always collect for overview Write-Output "Retrieving device scores..." if ($ShowProgress -and -not $IsAzureAutomation) { Write-Progress -Activity "Endpoint Analytics Report" -Status "Retrieving device scores" -PercentComplete 10 } try { $deviceScoresUri = "https://graph.microsoft.com/beta/deviceManagement/userExperienceAnalyticsDeviceScores?`$select=id,deviceName,model,endpointAnalyticsScore,startupPerformanceScore,appReliabilityScore,batteryHealthScore" $deviceScores = Get-MgGraphAllPage -Uri $deviceScoresUri $allMetrics['DeviceScores'] = $deviceScores Write-Output "✓ Retrieved $($deviceScores.Count) device score records" } catch { Write-Warning "Failed to retrieve device scores: $($_.Exception.Message)" $allMetrics['DeviceScores'] = @() } # Startup Performance if ($IncludeStartupPerformance) { Write-Output "Retrieving startup performance metrics..." if ($ShowProgress -and -not $IsAzureAutomation) { Write-Progress -Activity "Endpoint Analytics Report" -Status "Retrieving startup performance metrics" -PercentComplete 30 } try { $startupHistoryUri = "https://graph.microsoft.com/beta/deviceManagement/userExperienceAnalyticsDeviceStartupHistory" $startupHistory = Get-MgGraphAllPage -Uri $startupHistoryUri $allMetrics['StartupHistory'] = $startupHistory Write-Output "✓ Retrieved $($startupHistory.Count) startup history records" $devicePerformanceUri = "https://graph.microsoft.com/beta/deviceManagement/userExperienceAnalyticsDevicePerformance?`$select=id,deviceName,model,manufacturer,coreBootTimeInMs,groupPolicyBootTimeInMs" $devicePerformance = Get-MgGraphAllPage -Uri $devicePerformanceUri $allMetrics['DevicePerformance'] = $devicePerformance Write-Output "✓ Retrieved $($devicePerformance.Count) device performance records" } catch { Write-Warning "Failed to retrieve startup performance: $($_.Exception.Message)" $allMetrics['StartupHistory'] = @() $allMetrics['DevicePerformance'] = @() } } # Application Reliability if ($IncludeAppReliability) { Write-Output "Retrieving application reliability metrics..." if ($ShowProgress -and -not $IsAzureAutomation) { Write-Progress -Activity "Endpoint Analytics Report" -Status "Retrieving application reliability metrics" -PercentComplete 50 } try { $appHealthDeviceUri = "https://graph.microsoft.com/beta/deviceManagement/userExperienceAnalyticsAppHealthDevicePerformanceDetails" $appHealthDevice = Get-MgGraphAllPage -Uri $appHealthDeviceUri $allMetrics['AppHealthDevice'] = $appHealthDevice Write-Output "✓ Retrieved $($appHealthDevice.Count) app health device records" $appHealthAppUri = "https://graph.microsoft.com/beta/deviceManagement/userExperienceAnalyticsAppHealthApplicationPerformance?`$select=appDisplayName,appCrashCount,activeDeviceCount" $appHealthApp = Get-MgGraphAllPage -Uri $appHealthAppUri $allMetrics['AppHealthApplication'] = $appHealthApp Write-Output "✓ Retrieved $($appHealthApp.Count) app health application records" } catch { Write-Warning "Failed to retrieve application reliability: $($_.Exception.Message)" $allMetrics['AppHealthDevice'] = @() $allMetrics['AppHealthApplication'] = @() } } # Battery Health if ($IncludeBatteryHealth) { Write-Output "Retrieving battery health metrics..." if ($ShowProgress -and -not $IsAzureAutomation) { Write-Progress -Activity "Endpoint Analytics Report" -Status "Retrieving battery health metrics" -PercentComplete 70 } try { $batteryOsUri = "https://graph.microsoft.com/beta/deviceManagement/userExperienceAnalyticsBatteryHealthOsPerformance" $batteryOs = Get-MgGraphAllPage -Uri $batteryOsUri $allMetrics['BatteryOsPerformance'] = $batteryOs Write-Output "✓ Retrieved $($batteryOs.Count) battery OS performance records" $batteryDeviceUri = "https://graph.microsoft.com/beta/deviceManagement/userExperienceAnalyticsBatteryHealthDevicePerformance?`$select=id,deviceName,model,deviceBatteryHealthScore,maxCapacityPercentage,batteryAgeInDays" $batteryDevice = Get-MgGraphAllPage -Uri $batteryDeviceUri $allMetrics['BatteryDevicePerformance'] = $batteryDevice Write-Output "✓ Retrieved $($batteryDevice.Count) battery device performance records" } catch { Write-Warning "Failed to retrieve battery health: $($_.Exception.Message)" $allMetrics['BatteryOsPerformance'] = @() $allMetrics['BatteryDevicePerformance'] = @() } } # Work From Anywhere if ($IncludeWorkFromAnywhere) { Write-Output "Retrieving work from anywhere metrics..." if ($ShowProgress -and -not $IsAzureAutomation) { Write-Progress -Activity "Endpoint Analytics Report" -Status "Retrieving work from anywhere metrics" -PercentComplete 90 } try { $wfaUri = "https://graph.microsoft.com/beta/deviceManagement/userExperienceAnalyticsWorkFromAnywhereMetrics('allDevices')/metricDevices" $wfaMetrics = Get-MgGraphAllPage -Uri $wfaUri $allMetrics['WorkFromAnywhere'] = $wfaMetrics Write-Output "✓ Retrieved $($wfaMetrics.Count) work from anywhere records" } catch { Write-Warning "Failed to retrieve work from anywhere metrics: $($_.Exception.Message)" $allMetrics['WorkFromAnywhere'] = @() } } if ($ShowProgress -and -not $IsAzureAutomation) { Write-Progress -Activity "Endpoint Analytics Report" -Completed } # Create output directory if it does not exist if (-not (Test-Path $OutputPath)) { New-Item -Path $OutputPath -ItemType Directory -Force | Out-Null Write-Output "Created output directory: $OutputPath" } # Export results to CSV Write-Output "Exporting results to CSV..." foreach ($key in $allMetrics.Keys) { if ($allMetrics[$key].Count -gt 0) { $csvPath = Join-Path $OutputPath "EndpointAnalytics_${key}_$timestamp.csv" try { $allMetrics[$key] | Export-Csv -Path $csvPath -NoTypeInformation -Encoding utf8 Write-Output "✓ Exported $key to: $csvPath" } catch { Write-Warning "Failed to export $key to CSV: $($_.Exception.Message)" } } } # Export to JSON if requested if ($ExportJson) { Write-Output "Exporting results to JSON..." foreach ($key in $allMetrics.Keys) { if ($allMetrics[$key].Count -gt 0) { $jsonPath = Join-Path $OutputPath "EndpointAnalytics_${key}_$timestamp.json" try { $allMetrics[$key] | ConvertTo-Json -Depth 10 | Out-File -FilePath $jsonPath -Encoding utf8 Write-Output "✓ Exported $key to: $jsonPath" } catch { Write-Warning "Failed to export $key to JSON: $($_.Exception.Message)" } } } } # Calculate Analytics and Statistics Write-Output "Calculating analytics statistics..." # Device Scores Analytics $avgEndpointScore = Get-SafeAverage -InputObject @($allMetrics['DeviceScores']) -Property 'endpointAnalyticsScore' $avgStartupScore = Get-SafeAverage -InputObject @($allMetrics['DeviceScores']) -Property 'startupPerformanceScore' $avgAppReliabilityScore = Get-SafeAverage -InputObject @($allMetrics['DeviceScores']) -Property 'appReliabilityScore' # Startup Performance Analytics $slowBootDevices = @() $avgBootTime = 0 if ($IncludeStartupPerformance -and $allMetrics['DevicePerformance'].Count -gt 0) { # Calculate total boot time (core + group policy) $bootTimeRecords = @( $allMetrics['DevicePerformance'] | ForEach-Object { [pscustomobject]@{ totalBootTime = [double]$_.coreBootTimeInMs + [double]$_.groupPolicyBootTimeInMs } } ) $avgBootTime = Get-SafeAverage -InputObject $bootTimeRecords -Property 'totalBootTime' -Divisor 1000 $slowBootDevices = $allMetrics['DevicePerformance'] | Where-Object { ($_.coreBootTimeInMs + $_.groupPolicyBootTimeInMs) -gt 60000 } | Sort-Object { $_.coreBootTimeInMs + $_.groupPolicyBootTimeInMs } -Descending | Select-Object -First 10 deviceName, @{N='BootTimeSec';E={[math]::Round(($_.coreBootTimeInMs + $_.groupPolicyBootTimeInMs)/1000,1)}}, model, manufacturer } # App Reliability Analytics $topCrashingApps = @() if ($IncludeAppReliability -and $allMetrics['AppHealthApplication'].Count -gt 0) { $topCrashingApps = $allMetrics['AppHealthApplication'] | Where-Object { $_.appCrashCount -gt 0 } | Sort-Object appCrashCount -Descending | Select-Object -First 10 appDisplayName, appCrashCount, activeDeviceCount } # Battery Health Analytics $lowBatteryDevices = @() $avgBatteryHealth = 0 if ($IncludeBatteryHealth -and $allMetrics['BatteryDevicePerformance'].Count -gt 0) { $avgBatteryHealth = Get-SafeAverage -InputObject @($allMetrics['BatteryDevicePerformance']) -Property 'deviceBatteryHealthScore' $lowBatteryDevices = $allMetrics['BatteryDevicePerformance'] | Where-Object { $_.maxCapacityPercentage -lt 60 } | Sort-Object maxCapacityPercentage | Select-Object -First 10 deviceName, maxCapacityPercentage, batteryAgeInDays, model } # Generate HTML Summary Report Write-Output "Generating HTML summary report..." $htmlContent = @"
Generated on: $(Get-Date -Format "dddd, MMMM dd, yyyy 'at' HH:mm:ss")
Average boot time: $avgBootTime seconds
Devices with slow boot (>60s): $($slowBootDevices.Count)
Applications monitored: $($allMetrics['AppHealthApplication'].Count)
Apps with crashes: $($topCrashingApps.Count)
Devices monitored: $($allMetrics['BatteryDevicePerformance'].Count)
Devices with low battery (<60%): $($lowBatteryDevices.Count)
| Device Name | Model | Endpoint Score | Startup Score | App Reliability | Battery Health | Status |
|---|---|---|---|---|---|---|
| $($device.deviceName) | $($device.model) | $($device.endpointAnalyticsScore) | $($device.startupPerformanceScore) | $($device.appReliabilityScore) | $($device.batteryHealthScore) | $status |