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' )