Catesta.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# This is a locally sourced Imports file for local development.
# It can be imported by the psm1 in local development to add script level variables.
# It will merged in the build process. This is for local development only.

# region script variables
$script:resourcePath = "$PSScriptRoot\Resources"


<#
.EXTERNALHELP Catesta-help.xml
#>

function New-PowerShellProject {
    [CmdletBinding(ConfirmImpact = 'Low',
        SupportsShouldProcess = $true)]
    param (
        [Parameter(Mandatory = $true,
            HelpMessage = 'CICD Platform Choice')]
        [ValidateSet('AWS', 'GitHubActions', 'Azure', 'AppVeyor', 'ModuleOnly')]
        [string]
        $CICDChoice,
        [Parameter(Mandatory = $true,
            HelpMessage = 'File path where PowerShell Module project will be created')]
        [string]
        $DestinationPath,
        [Parameter(Mandatory = $false,
            HelpMessage = 'Skip confirmation')]
        [switch]$Force
    )
    Begin {

        if (-not $PSBoundParameters.ContainsKey('Verbose')) {
            $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference')
        }
        if (-not $PSBoundParameters.ContainsKey('Confirm')) {
            $ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference')
        }
        if (-not $PSBoundParameters.ContainsKey('WhatIf')) {
            $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference')
        }

        Write-Verbose -Message ('[{0}] Confirm={1} ConfirmPreference={2} WhatIf={3} WhatIfPreference={4}' -f $MyInvocation.MyCommand, $Confirm, $ConfirmPreference, $WhatIf, $WhatIfPreference)

    }#begin
    Process {
        # -Confirm --> $ConfirmPreference = 'Low'
        # ShouldProcess intercepts WhatIf* --> no need to pass it on
        if ($Force -or $PSCmdlet.ShouldProcess("ShouldProcess?")) {
            Write-Verbose -Message ('[{0}] Reached command' -f $MyInvocation.MyCommand)
            $ConfirmPreference = 'None'

            Write-Verbose -Message 'Importing Plaster...'
            try {
                Import-Module -Name Plaster -ErrorAction Stop
                Write-Verbose 'Plaster Imported.'
            }
            catch {
                throw $_
            }

            Write-Verbose -Message 'Sourcing correct template...'
            switch ($CICDChoice) {
                'AWS' {
                    Write-Verbose -Message 'AWS Template Selected.'
                    $path = '\AWS'
                }#aws
                'GitHubActions' {
                    Write-Verbose -Message 'GitHub Actions Template Selected.'
                    $path = '\GitHubActions'
                }#githubactions
                'Azure' {
                    Write-Verbose -Message 'Azure Pipelines Template Selected.'
                    $path = '\Azure'
                }#githubactions
                'AppVeyor' {
                    Write-Verbose -Message 'AppVeyor Template Selected.'
                    $path = '\AppVeyor'
                }#appveyor
                'ModuleOnly' {
                    Write-Verbose -Message 'Module Only Template Selected.'
                    $path = '\Vanilla'
                }#moduleonly
            }#switch

            Write-Verbose -Message 'Deploying template...'
            try {
                Write-Verbose -Message "Template Path: $script:resourcePath\$path"
                $invokePlasterSplat = @{
                    TemplatePath    = "$script:resourcePath\$path"
                    DestinationPath = $DestinationPath
                    VAULT           = 'NOTVAULT'
                    PassThru        = $true
                    ErrorAction     = 'Stop'
                }

                $results = Invoke-Plaster @invokePlasterSplat
                Write-Verbose -Message 'Template Deployed.'
            }
            catch {
                Write-Error $_
                $results = [PSCustomObject]@{
                    Success = $false
                }
            }

        }#if_Should
    }#process
    End {
        return $results
    }#end
}#New-PowerShellProject


<#
.EXTERNALHELP Catesta-help.xml
#>

function New-VaultProject {
    [CmdletBinding(ConfirmImpact = 'Low',
        SupportsShouldProcess = $true)]
    param (
        [Parameter(Mandatory = $true,
            HelpMessage = 'CICD Platform Choice')]
        [ValidateSet('AWS', 'GitHubActions', 'Azure', 'AppVeyor', 'ModuleOnly')]
        [string]
        $CICDChoice,
        [Parameter(Mandatory = $true,
            HelpMessage = 'File path where PowerShell SecretManagement vault module project will be created')]
        [string]
        $DestinationPath,
        [Parameter(Mandatory = $false,
            HelpMessage = 'Skip confirmation')]
        [switch]$Force
    )
    Begin {

        if (-not $PSBoundParameters.ContainsKey('Verbose')) {
            $VerbosePreference = $PSCmdlet.SessionState.PSVariable.GetValue('VerbosePreference')
        }
        if (-not $PSBoundParameters.ContainsKey('Confirm')) {
            $ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference')
        }
        if (-not $PSBoundParameters.ContainsKey('WhatIf')) {
            $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference')
        }

        Write-Verbose -Message ('[{0}] Confirm={1} ConfirmPreference={2} WhatIf={3} WhatIfPreference={4}' -f $MyInvocation.MyCommand, $Confirm, $ConfirmPreference, $WhatIf, $WhatIfPreference)

    }#begin
    Process {
        # -Confirm --> $ConfirmPreference = 'Low'
        # ShouldProcess intercepts WhatIf* --> no need to pass it on
        if ($Force -or $PSCmdlet.ShouldProcess("ShouldProcess?")) {
            Write-Verbose -Message ('[{0}] Reached command' -f $MyInvocation.MyCommand)
            $ConfirmPreference = 'None'

            Write-Verbose -Message 'Importing Plaster...'
            try {
                Import-Module -Name Plaster -ErrorAction Stop
                Write-Verbose 'Plaster Imported.'
            }
            catch {
                throw $_
            }

            Write-Verbose -Message 'Sourcing correct template...'
            switch ($CICDChoice) {
                'AWS' {
                    Write-Verbose -Message 'AWS Template Selected.'
                    $path = '\AWS\Vault'
                }#aws
                'GitHubActions' {
                    Write-Verbose -Message 'GitHub Actions Template Selected.'
                    $path = '\GitHubActions\Vault'
                }#githubactions
                'Azure' {
                    Write-Verbose -Message 'Azure Pipelines Template Selected.'
                    $path = '\Azure\Vault'
                }#githubactions
                'AppVeyor' {
                    Write-Verbose -Message 'AppVeyor Template Selected.'
                    $path = '\AppVeyor\Vault'
                }#appveyor
                'ModuleOnly' {
                    Write-Verbose -Message 'Module Only Template Selected.'
                    $path = '\Vanilla\Vault'
                }#moduleonly
            }#switch

            Write-Verbose -Message 'Deploying template...'
            try {
                Write-Verbose -Message "Template Path: $script:resourcePath\$path"
                $invokePlasterSplat = @{
                    TemplatePath    = "$script:resourcePath\$path"
                    DestinationPath = $DestinationPath
                    VAULT           = 'VAULT'
                    PassThru        = $true
                    ErrorAction     = 'Stop'
                }
                $results = Invoke-Plaster @invokePlasterSplat
                Write-Verbose -Message 'Template Deployed.'
            }
            catch {
                Write-Error $_
                $results = [PSCustomObject]@{
                    Success = $false
                }
            }

        }#if_Should
    }#process
    End {
        return $results
    }#end
}#New-VaultProject