Public/Get-LabRecipe.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
function Get-LabRecipe
{
    [CmdletBinding()]
    param
    (
        [Parameter()]
        [string[]]
        $Name,

        [Parameter()]
        [scriptblock]
        $RecipeContent
    )

    if ($RecipeContent)
    {
        if ($Name.Count -gt 1)
        {
            Write-PSFMessage -Level Warning -Message "Provided more than one name when using RecipeContent. Ignoring every value but the first."
        }

        $newScript = "[hashtable]@{$($RecipeContent.ToString())}"
        $newScriptBlock = [scriptblock]::Create($newScript)
        $table = & $newScriptBlock
        $mandatoryKeys = @(
            'Name'
            'DeployRole'
        )
        $allowedKeys = @(
            'Name'
            'Description'
            'RequiredProductIsos'
            'DeployRole'
            'DefaultVirtualizationEngine'
            'DefaultDomainName'
            'DefaultAddressSpace'
            'DefaultOperatingSystem'
            'VmPrefix'
        )

        $table.Name = $Name[0]
        $allowedKeys.ForEach({if (-not $table.ContainsKey($_)){$table.Add($_, $null)}})
        [bool] $shouldAlsoDeploySql = ($table.DeployRole -match 'CI_CD|DSCPull').Count -gt 0
        [bool] $shouldAlsoDeployDomain = ($table.DeployRole -match 'Exchange|PKI|DSCPull').Count -gt 0
        [bool] $shouldAlsoDeployPki = ($table.DeployRole -match 'CI_CD|DSCPull').Count -gt 0

        [string[]]$roles = $table.DeployRole.Clone()
        if ($shouldAlsoDeploySql -and $table.DeployRole -notcontains 'SQL') {$roles += 'SQL'}
        if ($shouldAlsoDeployDomain -and $table.DeployRole -notcontains 'Domain') {$roles += 'Domain'}
        if ($shouldAlsoDeployPki -and $table.DeployRole -notcontains 'PKI') {$roles += 'PKI'}
        $table.DeployRole = $roles

        $test = Test-HashtableKeys -Hashtable $table -ValidKeys $allowedKeys -MandatoryKeys $mandatoryKeys -Quiet

        if (-not $test) {}

        return ([pscustomobject]$table)
    }

    $recipePath = Join-Path -Path $HOME -ChildPath 'automatedLab\recipes'

    $recipes = Get-ChildItem -Path $recipePath

    if ($Name)
    {
        $recipes = $recipes | Where-Object -Property BaseName -in $Name
    }

    foreach ($recipe in $recipes)
    {
        $recipe | Get-Content -Raw | ConvertFrom-Json
    }
}