functions/stringMappings/Register-TmfStringMapping.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
41
42
43
44
45
46
function Register-TmfStringMapping
{
    [CmdletBinding(DefaultParameterSetName = 'IPRanges')]
    Param (
        [Parameter(Mandatory = $true)]
        [string] $name,

        [Parameter(Mandatory = $true)]
        [string] $replace,        

        [string] $sourceConfig = "<Custom>",

        [System.Management.Automation.PSCmdlet]
        $Cmdlet = $PSCmdlet
    )
    
    begin
    {        
        $resourceName = "stringMappings"
        if (!$script:desiredConfiguration[$resourceName]) {
            $script:desiredConfiguration[$resourceName] = @()
        }

        if ($script:desiredConfiguration[$resourceName].name -contains $name) {            
            $alreadyLoaded = $script:desiredConfiguration[$resourceName] | ? {$_.name -eq $name}
        }
    }
    process
    {
        if (Test-PSFFunctionInterrupt) { return }                

        $object = [PSCustomObject] @{
            name = $name
            replace = $replace
            sourceConfig = $sourceConfig
        }

        if ($alreadyLoaded) {
            $script:desiredConfiguration[$resourceName][$script:desiredConfiguration[$resourceName].IndexOf($alreadyLoaded)] = $object
        }
        else {
            $script:desiredConfiguration[$resourceName] += $object
        }        
    }
}