Scripts/Export-MultipleModules.ps1

#Requires -Version 5.1
<#
.SYNOPSIS
    Exports multiple PowerShell modules in parallel
     
.DESCRIPTION
    Wrapper script for parallel module exports with progress tracking.
    Provides a convenient interface for batch exporting modules matching
    a specific pattern.
     
.EXAMPLE
    .\Export-MultipleModules.ps1 -ModulePattern "VMware.*" -OutputDirectory "C:\Exports" -Format "JSON"
     
    This command exports all modules matching the pattern "VMware.*" to the
    specified output directory in JSON format.
#>


[CmdletBinding()]
param(
    [Parameter()]
    [string]$ModulePattern = "Microsoft.PowerShell.*",
    
    [Parameter()]
    [string]$OutputDirectory = ".\Exports",
    
    [Parameter()]
    [ValidateSet("JSON", "Markdown", "XML")]
    [string]$Format = "JSON",
    
    [Parameter()]
    [int]$MaxModules = 0,
    
    [Parameter()]
    [ValidateRange(1, 10)]
    [int]$MaxConcurrent = 3
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# Import the module
$modulePath = Split-Path -Parent $PSScriptRoot
Import-Module $modulePath -Force

Write-Host "`n=== PowerShell Module Batch Exporter ===" -ForegroundColor Cyan
Write-Host "Module Pattern: $ModulePattern" -ForegroundColor Gray
Write-Host "Output Directory: $OutputDirectory" -ForegroundColor Gray
Write-Host "Format: $Format" -ForegroundColor Gray

# Get modules matching the pattern
$modules = Get-Module -ListAvailable -Name $ModulePattern

# Limit the number of modules to export if MaxModules is specified
if ($MaxModules -gt 0) {
    $modules = $modules | Select-Object -First $MaxModules
}

if (-not $modules -or $modules.Count -eq 0) {
    Write-Warning "No modules found matching the pattern '$ModulePattern'"
    return
}

# Create output directory if it doesn't exist
if (-not (Test-Path $OutputDirectory)) {
    New-Item -Path $OutputDirectory -ItemType Directory -Force | Out-Null
}

# Export module commands in parallel
$results = Start-ParallelModuleExport -ModuleNames $modules.Name -OutputDirectory $OutputDirectory -Format $Format -MaxConcurrentModules $MaxConcurrent

# Display results
$results | ForEach-Object {
    if ($_.Success) {
        Write-Host "Successfully exported: $($_.ModuleName) to $($_.ExportPath)" -ForegroundColor Green
    } else {
        Write-Warning "Failed to export: $($_.ModuleName) - $($_.Error)"
    }
}