Tools/Rebuild-Module.ps1
|
# Rebuild-Module.ps1 # Reloads and validates all Public/ and Private/ scripts for the O365-Toolkit module. param( [switch]$RunScriptAnalyzer, [switch]$VerboseOutput ) function Write-Info { param($m) if ($VerboseOutput) { Write-Host $m } } $moduleRoot = Split-Path -Parent $MyInvocation.MyCommand.Path Set-Location $moduleRoot Write-Host "Rebuilding O365-Toolkit: reloading Public and Private scripts..." # Unload the module if loaded if (Get-Module -Name O365-Toolkit -ErrorAction SilentlyContinue) { Write-Host "Removing loaded O365-Toolkit module..." Remove-Module O365-Toolkit -Force -ErrorAction SilentlyContinue } # Try importing helpful modules (Entra Beta, Microsoft.Graph) if available foreach ($m in @('Microsoft.Entra.Beta','Microsoft.Graph')) { if (Get-Module -ListAvailable -Name $m) { try { Import-Module $m -ErrorAction Stop; Write-Info "Imported $m" } catch { Write-Host "Warning: failed to import $m: $_" -ForegroundColor Yellow } } else { Write-Info "Module $m not available locally" } } $errors = @() function DotSourceFiles($path) { $files = Get-ChildItem -Path $path -Filter '*.ps1' -Recurse -File foreach ($f in $files) { Write-Host "Loading $($f.FullName)" -ForegroundColor Cyan try { . $f.FullName } catch { $msg = "Failed to load $($f.FullName): $($_.Exception.Message)" Write-Host $msg -ForegroundColor Red $script:errors += $msg } } } DotSourceFiles -path (Join-Path $moduleRoot 'Public') DotSourceFiles -path (Join-Path $moduleRoot 'Private') # Also source the module psm1 to ensure module-level code runs try { Write-Host "Sourcing O365-Toolkit.psm1..." -ForegroundColor Cyan . (Join-Path $moduleRoot 'O365-Toolkit.psm1') } catch { $msg = "Failed to source O365-Toolkit.psm1: $($_.Exception.Message)" Write-Host $msg -ForegroundColor Red $errors += $msg } if ($RunScriptAnalyzer -and (Get-Command -Name Invoke-ScriptAnalyzer -ErrorAction SilentlyContinue)) { Write-Host "Running PSScriptAnalyzer against Public/ and Private/..." try { Invoke-ScriptAnalyzer -Path (Join-Path $moduleRoot 'Public'),(Join-Path $moduleRoot 'Private') -Recurse | Format-Table -AutoSize } catch { Write-Host "ScriptAnalyzer failed: $_" -ForegroundColor Yellow } } if ($errors.Count -eq 0) { Write-Host "Rebuild completed: all scripts loaded successfully." -ForegroundColor Green } else { Write-Host "Rebuild completed with errors:" -ForegroundColor Yellow $errors | ForEach-Object { Write-Host "- $_" } } Write-Host "You can now import the module with: Import-Module .\O365-Toolkit.psm1 -Force" |