Modules/EncodingHelpers/M365DSCStringEncoding.psm1

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
function Format-M365DSCString
{
    [CmdletBinding()]
    [OutputType([System.Collections.HashTable])]
    param(
        [Parameter(Mandatory = $true)]
        [System.Collections.Hashtable]
        $Properties,

        [Parameter(Mandatory = $true)]
        [System.String]
        $ResourceName
    )

    $InvalidCharacters = @(
        @{
            # Tilted Apostrophe
            InvalidCharacter = "[\u2019]"
            MainReplaceBy    = "'"
            CimReplaceBy     = "''"
        },
        @{
            # Tilted Left Quotes
            InvalidCharacter = "[\u201C]"
            MainReplaceBy    = "`""
            CimReplaceBy     = '"'
        },
        @{
            # Tilted Right Quotes
            InvalidCharacter = "[\u201D]"
            MainReplaceBy    = "`""
            CimReplaceBy     = '"'
        }
    )

    $DSCResource = Get-DSCResource -Module 'Microsoft365DSC' `
        -Name $ResourceName

    # For each invalid character, look for an instance in the string,
    # if an instance is found,
    $newProperties = @{}
    foreach ($key in $Properties.Keys)
    {
        $newProperties.Add($key, $Properties.$key)
        if (-not [System.String]::IsNullOrEmpty($newProperties.$key) -and `
            $newProperties.$key.GetType().ToString() -eq 'System.String')
        {
            # Obtain the type for this property from the module;
            $foundProperty = $DSCResource.Properties | Where-Object -FilterScript {$_.Name -eq $key}
            foreach ($entry in $InvalidCharacters)
            {
                $found = $newProperties.$key -match $entry.InvalidCharacter
                
                while ($found)
                {
                    if ($foundProperty.PropertyType -eq '[string]')
                    {
                        $newProperties.$key = $newProperties.$key -replace $entry.InvalidCharacter, $entry.MainReplaceBy
                    }
                    elseif ($foundProperty.PropertyType -like '`[MSFT_*`]')
                    {
                        # Case property is a CIMInstance
                        $newProperties.$key = $newProperties.$key -replace $entry.InvalidCharacter, $entry.CimReplaceBy
                    }
                    else
                    {
                        break
                    }
                    $found = $newProperties.$key -match $entry.InvalidCharacter
                }
            }
        }
    }

    return $newProperties
}