build-pe-script-launcher/lib/ModuleManager.psm1
Claude b06374a562 Initial commit: Build PE Script Launcher
Production-grade Windows PE builder with PowerShell 7 integration.

Features:
- PowerShell 7 integrated into WinPE environment
- Automatic script launcher for external media
- Configurable startup and fallback scripts
- Custom console colors
- Maximized console window on boot
- PowerShell module integration
- Windows ADK auto-installation
- Defender optimization for faster builds

Project Structure:
- Build-PE.ps1: Main build orchestrator
- config.json: Configuration file
- lib/: Build modules (Dependencies, WinPEBuilder, PowerShell7, ModuleManager, DefenderOptimization)
- resources/: Runtime scripts (Invoke-ScriptLauncher, Invoke-ScriptMenu, Driver-Manager)
- CLAUDE.md: Complete project documentation
- README.md: Quick start guide

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-12 16:39:43 -05:00

129 lines
3.3 KiB
PowerShell

function Install-ModuleToWinPE {
<#
.SYNOPSIS
Copies a PowerShell module from the repository to the WinPE image.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$ModuleName,
[Parameter(Mandatory = $true)]
[string]$SourcePath,
[Parameter(Mandatory = $true)]
[string]$MountDir
)
Write-Host " Installing module: $ModuleName" -ForegroundColor Cyan
if (-not (Test-Path $SourcePath)) {
throw "Module source path not found: $SourcePath"
}
$winpeModulesPath = Join-Path $MountDir "Program Files\WindowsPowerShell\Modules"
if (-not (Test-Path $winpeModulesPath)) {
New-Item -ItemType Directory -Path $winpeModulesPath -Force | Out-Null
}
$destModulePath = Join-Path $winpeModulesPath $ModuleName
if (Test-Path $destModulePath) {
Write-Host " Removing existing module installation..." -ForegroundColor Yellow
Remove-Item $destModulePath -Recurse -Force
}
try {
Copy-Item -Path $SourcePath -Destination $destModulePath -Recurse -Force
Write-Host "$ModuleName installed" -ForegroundColor Green
}
catch {
throw "Failed to copy module $ModuleName : $_"
}
$manifestPath = Get-ChildItem -Path $destModulePath -Filter "*.psd1" -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1
if ($manifestPath) {
Write-Verbose "Module manifest found: $($manifestPath.FullName)"
} else {
Write-Warning "No module manifest (.psd1) found for $ModuleName"
}
}
function Test-ModuleInRepository {
<#
.SYNOPSIS
Checks if a module exists in the repository's modules directory.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$ModuleName,
[Parameter(Mandatory = $false)]
[string]$ModulesRoot = ".\modules"
)
$modulePath = Join-Path $ModulesRoot $ModuleName
if (-not (Test-Path $modulePath)) {
return $false
}
$hasContent = (Get-ChildItem -Path $modulePath -Recurse -File -ErrorAction SilentlyContinue).Count -gt 0
return $hasContent
}
function Get-RepositoryModulePath {
<#
.SYNOPSIS
Returns the full path to a module in the repository.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$ModuleName,
[Parameter(Mandatory = $false)]
[string]$ModulesRoot = ".\modules"
)
$modulePath = Join-Path $ModulesRoot $ModuleName
if (-not (Test-Path $modulePath)) {
throw "Module not found in repository: $ModuleName (expected at: $modulePath)"
}
return $modulePath
}
function Get-InstalledModulesInImage {
<#
.SYNOPSIS
Lists all PowerShell modules installed in the WinPE image.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$MountDir
)
$winpeModulesPath = Join-Path $MountDir "Program Files\WindowsPowerShell\Modules"
if (-not (Test-Path $winpeModulesPath)) {
return @()
}
$modules = Get-ChildItem -Path $winpeModulesPath -Directory -ErrorAction SilentlyContinue
return $modules | Select-Object -ExpandProperty Name
}
Export-ModuleMember -Function @(
'Install-ModuleToWinPE',
'Test-ModuleInRepository',
'Get-RepositoryModulePath',
'Get-InstalledModulesInImage'
)