Template Module Generator Tool
<# .SYNOPSIS Template Module Generator Tool .DESCRIPTION This script provides an interactive tool to generate a basic structure for a PowerShell module, including the module manifest, public and private function files, and a basic test file. .NOTES File Name : TemplateModuleGenerator.ps1 Author : [Your Name] Prerequisite : PowerShell V5.1 or later Version : 1.0 Date : [Current Date] .EXAMPLE .\TemplateModuleGenerator.ps1 #> function Show-Menu { Clear-Host Write-Host "=== Template Module Generator Tool ===" -ForegroundColor Cyan Write-Host "1. Set Module Details" Write-Host "2. Add Public Function" Write-Host "3. Add Private Function" Write-Host "4. Generate Module Files" Write-Host "5. Exit" } function Set-ModuleDetails { $script:moduleName = Read-Host "Enter the name of the module" $script:moduleDescription = Read-Host "Enter a brief description of the module" $script:moduleAuthor = Read-Host "Enter the author's name" $script:moduleVersion = Read-Host "Enter the module version (e.g., 1.0.0)" Write-Host "Module details set successfully." -ForegroundColor Green } function Add-PublicFunction { $functionName = Read-Host "Enter the name of the public function" $script:publicFunctions += $functionName Write-Host "Public function '$functionName' added." -ForegroundColor Green } function Add-PrivateFunction { $functionName = Read-Host "Enter the name of the private function" $script:privateFunctions += $functionName Write-Host "Private function '$functionName' added." -ForegroundColor Green } function Generate-ModuleFiles { if ([string]::IsNullOrWhiteSpace($script:moduleName)) { Write-Host "Please set module details first." -ForegroundColor Yellow return } $modulePath = Join-Path -Path $PWD -ChildPath $script:moduleName New-Item -Path $modulePath -ItemType Directory -Force | Out-Null # Create module manifest $manifestPath = Join-Path -Path $modulePath -ChildPath "$($script:moduleName).psd1" New-ModuleManifest -Path $manifestPath ` -RootModule "$($script:moduleName).psm1" ` -ModuleVersion $script:moduleVersion ` -Author $script:moduleAuthor ` -Description $script:moduleDescription ` -FunctionsToExport $script:publicFunctions # Create module script file $modulePSM1Path = Join-Path -Path $modulePath -ChildPath "$($script:moduleName).psm1" $modulePSM1Content = @" # Dot source public/private functions `$public = @(Get-ChildItem -Path `$PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue) `$private = @(Get-ChildItem -Path `$PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue) foreach (`$import in @(`$public + `$private)) { try { . `$import.FullName } catch { Write-Error -Message "Failed to import function `$(`$import.FullName): `$_" } } # Export public functions Export-ModuleMember -Function `$public.BaseName "@ Set-Content -Path $modulePSM1Path -Value $modulePSM1Content # Create Public and Private folders New-Item -Path (Join-Path -Path $modulePath -ChildPath "Public") -ItemType Directory -Force | Out-Null New-Item -Path (Join-Path -Path $modulePath -ChildPath "Private") -ItemType Directory -Force | Out-Null # Create function files foreach ($function in $script:publicFunctions) { $functionPath = Join-Path -Path $modulePath -ChildPath "Public\$function.ps1" $functionContent = @" function $function { <# .SYNOPSIS Brief description of $function .DESCRIPTION Detailed description of $function .PARAMETER Param1 Description of Param1 .EXAMPLE $function -Param1 Value Explanation of the example .NOTES Additional notes about the function #> [CmdletBinding()] param ( [Parameter(Mandatory=`$true)] [string]`$Param1 ) begin { # Initialize any prerequisites } process { # Main function logic } end { # Cleanup or final actions } } "@ Set-Content -Path $functionPath -Value $functionContent } foreach ($function in $script:privateFunctions) { $functionPath = Join-Path -Path $modulePath -ChildPath "Private\$function.ps1" $functionContent = @" function $function { [CmdletBinding()] param ( [Parameter(Mandatory=`$true)] [string]`$Param1 ) # Function logic here } "@ Set-Content -Path $functionPath -Value $functionContent } # Create a basic test file $testsFolderPath = Join-Path -Path $modulePath -ChildPath "Tests" New-Item -Path $testsFolderPath -ItemType Directory -Force | Out-Null $testFilePath = Join-Path -Path $testsFolderPath -ChildPath "$($script:moduleName).Tests.ps1" $testFileContent = @" `$ModuleName = '$($script:moduleName)' `$ModuleManifestPath = "`$PSScriptRoot\..\`$ModuleName.psd1" Describe "`$ModuleName Module Tests" { It "Module manifest is valid" { Test-ModuleManifest -Path `$ModuleManifestPath | Should -Not -BeNullOrEmpty `$? | Should -Be `$true } It "Module can be imported" { Import-Module `$ModuleManifestPath Get-Module `$ModuleName | Should -Not -BeNullOrEmpty } # Add more tests for your functions here } "@ Set-Content -Path $testFilePath -Value $testFileContent Write-Host "Module files generated successfully at: $modulePath" -ForegroundColor Green } # Initialize variables $script:moduleName = "" $script:moduleDescription = "" $script:moduleAuthor = "" $script:moduleVersion = "" $script:publicFunctions = @() $script:privateFunctions = @() # Main program loop do { Show-Menu $choice = Read-Host "`nEnter your choice (1-5)" switch ($choice) { "1" { Set-ModuleDetails } "2" { Add-PublicFunction } "3" { Add-PrivateFunction } "4" { Generate-ModuleFiles } "5" { Write-Host "Exiting program..." -ForegroundColor Yellow; break } default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red } } if ($choice -ne "5") { Read-Host "`nPress Enter to continue..." } } while ($choice -ne "5")
This Template Module Generator Tool includes:
- A menu-driven interface for easy navigation.
- Functions to:
- Set module details (name, description, author, version)
- Add public functions
- Add private functions
- Generate module files
Key features:
- Module Manifest Generation:
- Creates a .psd1 file with basic module information
- Module Script File (.psm1) Generation:
- Creates a .psm1 file that dot-sources public and private functions
- Function File Generation:
- Creates separate .ps1 files for each public and private function
- Generates a basic function template with comment-based help for public functions
- Directory Structure Creation:
- Creates a module folder with Public and Private subfolders
- Basic Test File Generation:
- Creates a simple Pester test file for the module
This tool is particularly useful for:
- PowerShell module developers starting new projects
- Anyone learning to create PowerShell modules
- Developers who want to quickly scaffold a new module structure
To use this script effectively:
- Run the script in PowerShell
- Use the menu options to:
- Set the module details
- Add public and private functions as needed
- Generate the module files
- Review the generated module structure in the specified directory
This script provides a quick and easy way to create a basic PowerShell module structure, following best practices for module organization. It saves time in setting up the initial files and folders, allowing developers to focus on writing the actual module functionality.
Leave a Reply
Want to join the discussion?Feel free to contribute!