functions/Close-SBStringBuilder.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
function Close-SBStringBuilder {
<#
    .SYNOPSIS
        Closes out an open stringbuilder, returning its string value content.
     
    .DESCRIPTION
        Closes out an open stringbuilder, returning its string value content.
     
    .PARAMETER Name
        Name of the string builder to work with.
        Defaults to the caller's module name (if present) or '<none>' (if not so)
     
    .EXAMPLE
        PS C:\> Close-SBStringBuilder
     
        Close out the current stringbuilder object.
#>

    [OutputType([string])]
    [CmdletBinding()]
    param (
        [PsfArgumentCompleter('StringBuilder.Name')]
        [string]
        $Name
    )
    
    begin {
        $sbName = [PSFramework.Utility.UtilityHost]::FriendlyCallstack.Entries[1].InvocationInfo.MyCommand.Module.Name
        if (-not $sbName) { $sbName = '<none>' }
        if ($Name) { $sbName = $Name }
        
        Assert-StringBuilder -Name $sbName -Cmdlet $PSCmdlet
    }
    process {
        $script:builders[$sbName].ToString()
        $script:builders.Remove($sbName)
    }
}