Public/Resource Helpers/Add-UserData.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function Add-UserData {
    <#
    .SYNOPSIS
        Adds UserData to a resource on the template. For single values (i.e. in AutoScaling Launch Configurations), it adds the single For multiple values, it automatically adds it as {"Fn::Base64": {"Fn::Join": ["",[VALUES...] ] } } to reduce the amount of scripting needed.
    
    .PARAMETER String
        An array of strings and/or Instrinsic Functions.

        IMPORTANT: You must specify new lines in Powershell syntax so it identifies it as a new line when converting to JSON via Export-Vaporshell. This will convert `n [backtick n] into \n [backslash n] in the resulting JSON template.
    
    .PARAMETER File
        The path of the script file to convert to UserData. This cannot contain any Intrinsic functions such as Ref in it. Use the String parameter if you'd like to include functions in the array.

    .EXAMPLE
        $EC2 =

    .FUNCTIONALITY
        Vaporshell
    #>

    [OutputType('Vaporshell.Resource.UserData')]
    [cmdletbinding(DefaultParameterSetName="String")]
    Param
    (
        [parameter(Mandatory = $true,Position = 0,ParameterSetName="String")]
        [ValidateScript( {
                $allowedTypes = "System.String","Vaporshell.Function"
                if ([string]$($_.PSTypeNames) -match "($(($allowedTypes|ForEach-Object{[RegEx]::Escape($_)}) -join '|'))") {
                    $true
                }
                else {
                    throw "The Properties parameter only accepts the following types: $($allowedTypes -join ", "). The current types of the value are: $($_.PSTypeNames -join ", ")."
                }
            })]
        $String,
        [parameter(Mandatory = $true,Position = 0,ParameterSetName="File")]
        [ValidateScript( {
                if (Test-Path $_) {
                    $true
                }
                else {
                    throw "You must specify a valid file path -- unable to find the path $_"
                }
            })]
        [System.String]
        $File
    )
    Begin {
        $Values = @()
    }
    Process {
        switch ($PSBoundParameters.Keys) {
            'String' {
                $Values = $String
            }
            'File' {
                $Path = (Resolve-Path -Path $File).Path
                if ($Path -like "*.ps1") {
                    $Windows = $true
                    $tag = "powershell"
                }
                elseif ($Path -like "*.bat" -or $Path -like "*.cmd") {
                    $Windows = $true
                    $tag = "script"
                }
                else {
                    $Windows = $false
                }
                [System.Collections.ArrayList]$fileContents = Get-Content $Path
                do {
                    if ([string]::IsNullOrWhiteSpace($fileContents[0])){
                        $fileContents.RemoveAt(0)
                    }
                } until (!([string]::IsNullOrWhiteSpace($fileContents[0])))
                do {
                    $lastIndex = $fileContents.Count - 1
                    if ([string]::IsNullOrWhiteSpace($fileContents[$lastIndex])){
                        $fileContents.RemoveAt($lastIndex)
                    }
                } until (!([string]::IsNullOrWhiteSpace($fileContents[$lastIndex - 1])))
                if ($Windows) {
                    if ($fileContents[0] -notlike "<$($tag)>*") {
                        $Values += "<$($tag)>"
                    }
                }
                $fileContents | ForEach-Object {
                    $Values += "$($_)`n"
                }
                if ($Windows) {
                    if ($fileContents[$fileContents.Count - 1] -notlike "</$($tag)>*") {
                        $Values += "</$($tag)>"
                    }
                }
            }
        }
    }
    End {
        if ($Values.Count -gt 1) {
            $obj = Add-FnBase64 -ValueToEncode (Add-FnJoin -ListOfValues $Values)
        }
        else {
            $obj = Add-FnBase64 -ValueToEncode ($Values | Select-Object -First 1)
        }
        $obj | Add-ObjectDetail -TypeName 'Vaporshell.Resource.UserData'
        Write-Verbose "Resulting JSON from $($MyInvocation.MyCommand): `n`n`t$($obj | ConvertTo-Json -Depth 5)`n"
    }
}