Snippets/internet.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
$snippet = {
    param
    (
        [Parameter(Mandatory, ParameterSetName = 'NoDefaultSwitch')]
        [switch]
        $NoDefaultSwitch,

        [Parameter(Mandatory, ParameterSetName = 'NoDefaultSwitch')]
        [string]
        $AdapterName
    )

    $externalNetworkName, $adapter = if ($NoDefaultSwitch)
    {
        '{0}EXT' -f (Get-LabDefinition).Name
        $AdapterName
    }
    else
    {
        'Default switch'
        'Ethernet' # unnecessary but required
    }

    Add-LabVirtualNetworkDefinition -Name $externalNetworkName -HyperVProperties @{ SwitchType = 'External'; AdapterName = $adapter }

    $adapters = @(
        New-LabNetworkAdapterDefinition -VirtualSwitch (Get-LabDefinition).Name
        New-LabNetworkAdapterDefinition -VirtualSwitch $externalNetworkName -UseDhcp
    )

    $router = Add-LabMachineDefinition -Name ('{0}GW01' -f $AutomatedLabVmNamePrefix) -Roles Routing -NetworkAdapter $adapters -PassThru
    $PSDefaultParameterValues['Add-LabMachineDefinition:Gateway'] = $router.NetworkAdapters.Where( { $_.VirtualSwitch.ResourceName -eq (Get-LabDefinition).Name }).Ipv4Address.IpAddress.ToString()
}

New-LabSnippet -Name InternetConnectivity -Description 'Basic snippet to add a router and external switch to the lab' -Tag Definition, Routing, Internet -Type Snippet -ScriptBlock $snippet -DependsOn LabDefinition -NoExport -Force