Public/New-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
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 New-LabRecipe
{
    [CmdletBinding(SupportsShouldProcess)]
    param
    (
        # Name of the lab and recipe
        [Parameter(Mandatory)]
        [string]
        $Name,

        # Description of lab
        [Parameter()]
        [string]
        $Description,

        [Parameter()]
        [string]
        $VmPrefix,

        # Roles this recipe deploys
        [Parameter(Mandatory)]
        [ValidateSet(
            'Domain',
            'PKI',
            'SQL',
            'Exchange',
            'CI_CD',
            'DSCPull'
        )]
        [string[]]
        $DeployRole,

        [Parameter()]
        [ValidateSet('HyperV', 'Azure', 'VMWare')]
        [string]
        $DefaultVirtualizationEngine,

        [Parameter()]
        [string]
        $DefaultDomainName,

        [Parameter()]
        [AutomatedLab.IPNetwork]
        $DefaultAddressSpace,

        [Parameter()]
        [AutomatedLab.OperatingSystem]
        $DefaultOperatingSystem,

        [switch]
        $Force,

        [switch]
        $PassThru
    )

    $labContent = @{
        Name                        = $Name
        Description                 = $Description
        RequiredProductIsos         = @()
        DeployRole                  = $DeployRole
        DefaultVirtualizationEngine = if ($DefaultVirtualizationEngine) {$DefaultVirtualizationEngine} else {'HyperV'}
        DefaultDomainName           = if ($DefaultDomainName) {$DefaultDomainName} else {'contoso.com'}
        DefaultAddressSpace         = if ($DefaultAddressSpace) {$DefaultAddressSpace.ToString()} else {'192.168.99.99/24'}
        DefaultOperatingSystem      = if ($DefaultOperatingSystem) {$DefaultOperatingSystem.OperatingSystemName} else {'Windows Server 2016 Datacenter'}
        VmPrefix                    = if ($VmPrefix) {$VmPrefix} else {(1..4 | ForEach-Object { [char[]](65..90) | Get-Random }) -join ''}
    }

    [bool] $shouldAlsoDeploySql = ($DeployRole -match 'CI_CD|DSCPull').Count -gt 0
    [bool] $shouldAlsoDeployDomain = ($DeployRole -match 'Exchange|PKI|DSCPull').Count -gt 0
    [bool] $shouldAlsoDeployPki = ($DeployRole -match 'CI_CD|DSCPull').Count -gt 0

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

    foreach ($role in $roles)
    {
        if ($role -notin 'Domain', 'PKI', 'DscPull')
        {
            $labContent.RequiredProductIsos += $role
        }
    }

    if (-not (Test-Path -Path (Join-Path -Path $HOME -ChildPath 'automatedlab\recipes')))
    {
        $null = New-Item -ItemType Directory -Path (Join-Path -Path $HOME -ChildPath 'automatedlab\recipes')
    }
    $recipeFileName = Join-Path -Path $HOME -ChildPath "automatedLab\recipes\$Name.json"

    if ((Test-Path -Path $recipeFileName) -and -not $Force.IsPresent)
    {
        Write-PSFMessage -Level Warning -Message "$recipeFileName exists and -Force was not used. Not storing recipe."
        return
    }

    if ($PSCmdlet.ShouldProcess($recipeFileName, 'Storing recipe'))
    {
        $labContent | ConvertTo-Json | Set-Content -Path $recipeFileName -NoNewline -Force
    }

    if ($PassThru) {$labContent}
}