Public/New-ClientVM.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
#requires -Modules "Hyper-V"
function New-ClientVM {
    [CmdletBinding(SupportsShouldProcess)]
    param (
        [parameter(Position = 1, Mandatory = $true)]
        [string]$TenantName,

        [parameter(Position = 2, Mandatory = $true)]
        [ValidateRange(1, 999)]
        [string]$NumberOfVMs,

        [parameter(Position = 3, Mandatory = $true)]
        [ValidateRange(1, 999)]
        [string]$CPUsPerVM,

        [parameter(Position = 4, Mandatory = $true)]
        [ValidateRange(2gb, 20gb)]
        [int64]$VMMemory,

        [parameter(Position = 5, Mandatory = $false)]
        [switch]$SkipAutoPilot
    )

    #region Config
    $clientDetails = $script:hvConfig.tenantConfig | Where-Object { $_.TenantName -eq $TenantName }
    $imageDetails = $script:hvConfig.images | Where-Object { $_.imageName -eq $clientDetails.imageName }
    $clientPath = "$($script:hvConfig.vmPath)\$($TenantName)"
    if (!(Test-Path $clientPath)) {
        New-Item -ItemType Directory -Force -Path $clientPath | Out-Null
    }

    Write-Verbose "Autopilot Reference VHDX: $($imageDetails.refImagePath)"
    Write-Verbose "Client name: $TenantName"
    Write-Verbose "Win10 ISO is located: $($imageDetails.imagePath)"
    Write-Verbose "Path to client VMs will be: $clientPath"
    Write-Verbose "Number of VMs to create: $NumberOfVMs"
    Write-Verbose "Admin user for $TenantName is: $($clientDetails.adminUpn)`n"
    #endregion

    #region Check for ref image - if it's not there, build it
    if (!(Test-Path -path $imageDetails.refImagePath -ErrorAction SilentlyContinue)) {
        Write-Host "Creating reference Autopilot VHDX - this may take some time.." -ForegroundColor Yellow -NoNewline
        New-ClientVHDX -vhdxpath $imageDetails.refImagePath -winiso $imageDetails.imagePath
        Write-Host "Reference Autopilot VHDX has been created.." -ForegroundColor Yellow
    }
    #endregion
    #region Get Autopilot policy
    if (!($SkipAutoPilot)) {
        Write-Host "Grabbing Autopilot config.." -ForegroundColor Yellow
        Get-AutopilotPolicy -FileDestination "$clientPath"
    }
    #endregion
    #region Build the client VMs
    if (!(Test-Path -Path $clientPath -ErrorAction SilentlyContinue)) {
        New-Item -Path $clientPath -ItemType Directory -Force | Out-Null
    }
    $vmParams = @{
        ClientPath  = $clientPath
        RefVHDX     = $imageDetails.refImagePath
        VSwitchName = $script:hvConfig.vSwitchName
        CPUCount    = $CPUsPerVM
        VMMMemory   = $VMMemory
    }
    if ($SkipAutoPilot) {
        $vmParams.skipAutoPilot = $true
    }
    if ($script:hvConfig.vLanId) {
        $vmParams.VLanId = $script:hvConfig.vLanId
    }
    if ($numberOfVMs -eq 1) {
        $max = ((Get-VM -Name "$TenantName*").name -replace "$TenantName`_" | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum) + 1
        $vmParams.VMName = "$($TenantName)_$max"
        Write-Host "Creating VM: $($vmParams.VMName).." -ForegroundColor Yellow
        New-ClientDevice @vmParams
    }
    else {
        (1..$NumberOfVMs) | ForEach-Object {
            $max = ((Get-VM -Name "$TenantName*").name -replace "$TenantName`_" | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum) + 1
            $vmParams.VMName = "$($TenantName)_$max"
            Write-Host "Creating VM: $($vmParams.VMName).." -ForegroundColor Yellow
            New-ClientDevice @vmParams
        }
    }
    #endregion
}