Public/Get-AutopilotDevicePreparationDiagnostic.ps1

function Get-AutopilotDevicePreparationDiagnostic {
    <#
    .SYNOPSIS
        Collects local Windows Autopilot device preparation diagnostics.
    .DESCRIPTION
        Reads the local Autopilot profile registry values, MDM enrollment summary,
        device preparation tracking, supported event channels, and IME metadata.
        It does not require Microsoft Graph credentials and makes no system changes.
    #>

    [CmdletBinding()]
    param(
        [ValidateRange(1, 5000)]
        [int]$EventLogLimit = 200,

        [datetime]$Since = (Get-Date).AddDays(-7),

        [switch]$IncludeEventMessage,

        [switch]$AsJson
    )

    $operatingSystem = $null
    try {
        $operatingSystem = Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction Stop
    } catch {
        $fallbackVersion = [Environment]::OSVersion.Version
        $operatingSystem = [pscustomobject]@{
            Caption       = $null
            Version       = $fallbackVersion.ToString()
            BuildNumber   = $fallbackVersion.Build.ToString()
            OSArchitecture = if ([Environment]::Is64BitOperatingSystem) { '64-bit' } else { '32-bit' }
        }
    }

    $autopilotProfile = Get-ADPDRegistrySnapshot -Path 'HKLM:\SOFTWARE\Microsoft\Provisioning\Diagnostics\Autopilot' -Name @(
        'AadTenantId',
        'CloudAssignedTenantDomain',
        'CloudAssignedTenantId',
        'CloudAssignedDeviceName',
        'CloudAssignedOobeConfig',
        'CloudAssignedForcedEnrollment'
    )

    $enrollmentKeys = @()
    try {
        $enrollmentKeys = @(Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Enrollments' -ErrorAction Stop |
                Where-Object { $_.PSChildName -match '^[0-9a-fA-F-]{36}$' } |
                ForEach-Object {
                    $entry = Get-ItemProperty -Path $_.PSPath -ErrorAction SilentlyContinue
                    [pscustomobject]@{
                        Id             = $_.PSChildName
                        EnrollmentType = $entry.EnrollmentType
                        DiscoveryServiceFullURL = $entry.DiscoveryServiceFullURL
                    }
                })
    } catch {
        $enrollmentKeys = @()
    }

    $eventLogNames = @(
        'Microsoft-Windows-ModernDeployment-Diagnostics-Provider/Autopilot',
        'Microsoft-Windows-DeviceManagement-Enterprise-Diagnostics-Provider/Admin',
        'Microsoft-Windows-Provisioning-Diagnostics-Provider/Admin',
        'Microsoft-Windows-AAD/Operational'
    )
    $eventLogs = @($eventLogNames | ForEach-Object {
            Get-ADPDEventLog -LogName $_ -Since $Since -MaximumEvents $EventLogLimit -IncludeMessage $IncludeEventMessage.IsPresent
        })

    $result = [pscustomobject]@{
        PSTypeName               = 'AutopilotPrepDiag.Snapshot'
        CollectedAt              = Get-Date
        ComputerName             = $env:COMPUTERNAME
        OperatingSystem          = [pscustomobject]@{
            Caption     = $operatingSystem.Caption
            Version     = $operatingSystem.Version
            BuildNumber = $operatingSystem.BuildNumber
            Architecture = $operatingSystem.OSArchitecture
        }
        AutopilotProfile         = $autopilotProfile
        Enrollment               = $enrollmentKeys
        EventLogs                = $eventLogs
        DevicePreparationTracking = Get-ADPDDevicePreparationTracking
        IntuneManagementExtension = Get-ADPDIntuneManagementExtension
    }

    if ($AsJson) {
        return ($result | ConvertTo-Json -Depth 8)
    }

    $result
}