Debug-EntraIDReport.ps1

<#
.SYNOPSIS
    Standalone debug helper for AsBuiltReport.Microsoft.EntraID.
    Runs the report with maximum verbosity and captures PScribo internals.
 
.DESCRIPTION
    Use this script when New-AsBuiltReport crashes with 'Index was outside the bounds
    of the array'. It wraps the report run with:
      - Full PowerShell transcript
      - PScribo document structure audit (empty sections/tables)
      - Verbose output from New-AsBuiltReport
      - Error log written to C:\Reports
 
.EXAMPLE
    # Run from the module folder:
    .\Debug-EntraIDReport.ps1
 
.NOTES
    Edit the $Config section below before running.
#>


#--------------------------------------------------------------------
# CONFIG -- edit these
#--------------------------------------------------------------------
$TenantDomain  = 'M365x29024066.onmicrosoft.com'
$AdminUPN      = 'admin@contoso.onmicrosoft.com'
$OutputFolder  = 'C:\Reports'
$ModuleFolder  = $PSScriptRoot   # folder containing AsBuiltReport.Microsoft.EntraID.psd1
#--------------------------------------------------------------------

$DebugLogPath = Join-Path $OutputFolder "EntraID_DEBUG_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
Start-Transcript -Path $DebugLogPath -Force | Out-Null
Write-Host "=== EntraID Report Debug Run ===" -ForegroundColor Cyan
Write-Host " Tenant : $TenantDomain"
Write-Host " UPN : $AdminUPN"
Write-Host " Output : $OutputFolder"
Write-Host " Log : $DebugLogPath"
Write-Host ""

# 1. Show PS version and module paths
Write-Host "--- PowerShell Environment ---" -ForegroundColor Yellow
Write-Host " PSVersion : $($PSVersionTable.PSVersion) ($($PSVersionTable.PSEdition))"
Write-Host " PSModulePath entries:"
$env:PSModulePath -split ';' | ForEach-Object { Write-Host " $_" }
Write-Host ""

# 2. Show loaded + available Graph modules
Write-Host "--- Microsoft.Graph Module Status ---" -ForegroundColor Yellow
$Required = @(
    'Microsoft.Graph.Authentication',
    'Microsoft.Graph.Identity.DirectoryManagement',
    'Microsoft.Graph.Users',
    'Microsoft.Graph.Groups',
    'Microsoft.Graph.Applications',
    'Microsoft.Graph.Identity.SignIns',
    'Microsoft.Graph.Identity.Governance'
)
foreach ($m in $Required) {
    $loaded    = Get-Module -Name $m -ErrorAction SilentlyContinue
    $available = Get-Module -ListAvailable -Name $m -ErrorAction SilentlyContinue |
                     Sort-Object Version -Descending | Select-Object -First 1
    $loadedStr    = if ($loaded)    { "[LOADED v$($loaded.Version)]" }    else { '[NOT LOADED]' }
    $availableStr = if ($available) { "[AVAIL v$($available.Version) @ $($available.ModuleBase)]" } else { '[NOT INSTALLED]' }
    $color = if ($loaded) { 'Green' } elseif ($available) { 'Yellow' } else { 'Red' }
    Write-Host " $m" -ForegroundColor $color
    Write-Host " $loadedStr $availableStr"
}
Write-Host ""

# 3. Force-import required modules
Write-Host "--- Force-importing Graph modules ---" -ForegroundColor Yellow
foreach ($m in $Required) {
    try {
        Import-Module $m -Force -Global -ErrorAction Stop
        Write-Host " [OK] $m" -ForegroundColor Green
    } catch {
        Write-Host " [FAIL] $m : $($_.Exception.Message)" -ForegroundColor Red
    }
}
Write-Host ""

# 4. Import the EntraID report module from local folder
Write-Host "--- Importing AsBuiltReport.Microsoft.EntraID ---" -ForegroundColor Yellow
try {
    Import-Module (Join-Path $ModuleFolder 'AsBuiltReport.Microsoft.EntraID.psd1') -Force -Global -ErrorAction Stop
    Write-Host " [OK] Module imported from $ModuleFolder" -ForegroundColor Green
} catch {
    Write-Host " [FAIL] $($_.Exception.Message)" -ForegroundColor Red
    Stop-Transcript | Out-Null
    exit 1
}
Write-Host ""

# 5. Set up report config JSON pointing to local folder
$ReportConfigPath = Join-Path $ModuleFolder 'AsBuiltReport.Microsoft.EntraID.json'
$BaseConfigPath   = Join-Path $ModuleFolder 'AsBuiltReport.json'

# 6. Run the report with -Verbose and capture all errors
Write-Host "--- Running New-AsBuiltReport (Verbose) ---" -ForegroundColor Yellow
Write-Host " If it crashes, check $DebugLogPath and C:\Reports\EntraID_Errors_*.log"
Write-Host ""

try {
    New-AsBuiltReport -Report 'Microsoft.EntraID' `
        -Target $TenantDomain `
        -Username $AdminUPN `
        -OutputFolderPath $OutputFolder `
        -ReportConfigFilePath $ReportConfigPath `
        -AsBuiltConfigFilePath $BaseConfigPath `
        -Format Word `
        -EnableHealthCheck `
        -Verbose `
        -ErrorAction Stop
    Write-Host ""
    Write-Host "=== Report completed successfully ===" -ForegroundColor Green
} catch {
    Write-Host ""
    Write-Host "=== CRASH DETECTED ===" -ForegroundColor Red
    Write-Host " Error : $($_.Exception.Message)" -ForegroundColor Red
    Write-Host " Type : $($_.Exception.GetType().FullName)" -ForegroundColor Red
    Write-Host " Position: $($_.InvocationInfo.PositionMessage)" -ForegroundColor Red
    Write-Host ""
    Write-Host " Stack trace:" -ForegroundColor Red
    $_.ScriptStackTrace -split "`n" | ForEach-Object { Write-Host " $_" -ForegroundColor DarkRed }
    Write-Host ""
    Write-Host " Check these files for details:" -ForegroundColor Yellow
    Write-Host " $DebugLogPath"
    Get-ChildItem $OutputFolder -Filter 'EntraID_Errors_*.log' -ErrorAction SilentlyContinue |
        Sort-Object LastWriteTime -Descending | Select-Object -First 1 |
        ForEach-Object { Write-Host " $($_.FullName)" }
}

Stop-Transcript | Out-Null
Write-Host ""
Write-Host "Full debug log saved to: $DebugLogPath" -ForegroundColor Cyan