Install.ps1

<#
.SYNOPSIS
    Installation helper for AsBuiltReport.Microsoft.SharePoint
.DESCRIPTION
    Checks for and installs required PowerShell module dependencies.
    Run this script before generating your first report.
.NOTES
    Version: 0.1.0
    Author: Pai Wei Sing
#>


[CmdletBinding(SupportsShouldProcess)]
param(
    [switch]$Force
)

Write-Host ""
Write-Host "AsBuiltReport.Microsoft.SharePoint -- Dependency Installer" -ForegroundColor Cyan
Write-Host "============================================================" -ForegroundColor Cyan
Write-Host ""

$RequiredModules = @(
    @{ Name = 'AsBuiltReport.Core'; Description = 'AsBuiltReport core framework (REQUIRED)' }
    @{ Name = 'Microsoft.Graph';    Description = 'Microsoft Graph SDK for PowerShell (REQUIRED)' }
    @{ Name = 'PnP.PowerShell';     Description = 'PnP PowerShell for SharePoint Online tenant management (HIGHLY RECOMMENDED)' }
    @{ Name = 'ImportExcel';        Description = 'ImportExcel for Excel report export (OPTIONAL)' }
)

foreach ($Mod in $RequiredModules) {
    $Installed = Get-Module -ListAvailable -Name $Mod.Name -ErrorAction SilentlyContinue |
        Sort-Object Version -Descending | Select-Object -First 1

    if ($Installed) {
        Write-Host " [OK] $($Mod.Name) v$($Installed.Version) -- $($Mod.Description)" -ForegroundColor Green
    } else {
        Write-Host " [MISSING] $($Mod.Name) -- $($Mod.Description)" -ForegroundColor Yellow
        if ($PSCmdlet.ShouldProcess($Mod.Name, "Install-Module")) {
            try {
                Write-Host " Installing $($Mod.Name)..." -ForegroundColor Cyan
                Install-Module -Name $Mod.Name -Force:$Force -Scope CurrentUser -ErrorAction Stop
                Write-Host " [INSTALLED] $($Mod.Name)" -ForegroundColor Green
            } catch {
                Write-Host " [FAILED] Could not install $($Mod.Name): $($_.Exception.Message)" -ForegroundColor Red
            }
        }
    }
}

Write-Host ""
Write-Host "Installation complete." -ForegroundColor Cyan
Write-Host ""
Write-Host "Next steps:" -ForegroundColor White
Write-Host " 1. Copy AsBuiltReport.Microsoft.SharePoint.json to your working directory" -ForegroundColor White
Write-Host " 2. Edit the JSON file: set Options.UserPrincipalName to your admin UPN" -ForegroundColor White
Write-Host " (Optional) Set Options.TenantAdminUrl if auto-detection doesn't work" -ForegroundColor White
Write-Host " 3. Run the report:" -ForegroundColor White
Write-Host " New-AsBuiltReport -Report 'Microsoft.SharePoint' -Target 'contoso.onmicrosoft.com' -OutputFolderPath C:\Reports -OutputFormat Word" -ForegroundColor DarkCyan
Write-Host ""