Private/New-PstEndBlock.ps1
|
function New-PstEndBlock { <# .SYNOPSIS Generates PowerShell end block code. .DESCRIPTION Creates a formatted end block with cleanup code and debug output based on complexity level. .PARAMETER FunctionName The name of the function (for debug messages). .PARAMETER Complexity The complexity level: Basic, Standard, or Advanced. .EXAMPLE New-PstEndBlock -FunctionName "Get-UserData" -Complexity Standard Generates a Standard complexity end block. .NOTES Version: 1.0 Author: numidia Creation Date: 2025-12-01 Purpose: Generate end blocks for functions #> [CmdletBinding()] [OutputType([string])] param( [Parameter(Mandatory = $true)] [string]$FunctionName, [Parameter(Mandatory = $true)] [ValidateSet('Basic', 'Standard', 'Advanced')] [string]$Complexity ) begin { Write-Debug "Begin '$($MyInvocation.MyCommand.Name)' at '$(Get-Date)'" } process { try { $features = Get-PstComplexityFeatures -Complexity $Complexity -Type Function # Basic doesn't use end block if (-not $features.Structure.EndBlock) { return '' } $sb = [System.Text.StringBuilder]::new() [void]$sb.AppendLine(' end {') [void]$sb.AppendLine(' Write-Debug "End ''$($MyInvocation.MyCommand.Name)'' at ''$(Get-Date)''"') [void]$sb.AppendLine() [void]$sb.AppendLine(' # Cleanup code here') [void]$sb.Append(' }') Write-Verbose "Generated $Complexity end block for '$FunctionName'" return $sb.ToString() } catch { Write-Error "Failed to generate end block: $($_.Exception.Message)" $PSCmdlet.ThrowTerminatingError($_) } } end { Write-Debug "End '$($MyInvocation.MyCommand.Name)' at '$(Get-Date)'" } } |