internal/functions/Assert-StringBuilder.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function Assert-StringBuilder
{
<#
    .SYNOPSIS
        Asserts a valid stringbuilder has been created.
     
    .DESCRIPTION
        Asserts a valid stringbuilder has been created.
     
    .PARAMETER Name
        Name of the stringbuilder that must exist.
     
    .PARAMETER Cmdlet
        The $PSCmdlet variable of the calling command.
        Causes the error record to be created in the context of that command
     
    .EXAMPLE
        PS C:\> Assert-StringBuilder -Name 'MyModule' -Cmdlet $Cmdlet
     
        Ensures that the string builder 'MyModule' exists.
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]
        $Name,
        
        [Parameter(Mandatory = $true)]
        $Cmdlet
    )
    
    process
    {
        if ($script:builders[$Name]) { return }
        
        $exception = [Exception]::new("String builder '$Name' not found! Ensure a proper string builder has been created!")
        $record = [System.Management.Automation.ErrorRecord]::new($exception, 'MissingStringBuilder', [System.Management.Automation.ErrorCategory]::InvalidOperation, $null)
        $Cmdlet.ThrowTerminatingError($record)
    }
}