Private/New-PstBeginBlock.ps1
|
function New-PstBeginBlock { <# .SYNOPSIS Generates PowerShell begin block code. .DESCRIPTION Creates a formatted begin block with initialization code 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-PstBeginBlock -FunctionName "Get-UserData" -Complexity Standard Generates a Standard complexity begin block. .NOTES Version: 1.0 Author: numidia Creation Date: 2025-12-01 Purpose: Generate begin 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 begin block if (-not $features.Structure.BeginBlock) { return '' } $sb = [System.Text.StringBuilder]::new() [void]$sb.AppendLine(' begin {') [void]$sb.AppendLine(' Write-Debug "Begin ''$($MyInvocation.MyCommand.Name)'' at ''$(Get-Date)''"') [void]$sb.AppendLine() [void]$sb.AppendLine(' # Initialization code here') [void]$sb.Append(' }') Write-Verbose "Generated $Complexity begin block for '$FunctionName'" return $sb.ToString() } catch { Write-Error "Failed to generate begin block: $($_.Exception.Message)" $PSCmdlet.ThrowTerminatingError($_) } } end { Write-Debug "End '$($MyInvocation.MyCommand.Name)' at '$(Get-Date)'" } } |