Public/Add-ImageToConfig.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
function Add-ImageToConfig {
    [cmdletbinding()]
    param (
        [parameter(Position = 1, Mandatory = $true)]
        $ImageName,
        [parameter(Mandatory = $true, ParameterSetName = 'ISO')]
        $IsoPath,
        [Parameter(Mandatory = $true, ParameterSetName = 'RefVHDX')]
        $ReferenceVHDX
    )
    try {
        Write-Host "Adding $ImageName to config.. " -ForegroundColor Cyan -NoNewline
        if(!$PSBoundParameters.ContainsKey('ReferenceVHDX')){
            $ReferenceVHDX = "$($script:hvConfig.vmPath)\wks$($ImageName)ref.vhdx"
        }
        $newTenant = [pscustomobject]@{
            imageName    = $ImageName
            imagePath    = $IsoPath
            refImagePath = $ReferenceVHDX
        }
        $script:hvConfig.images += $newTenant
        $script:hvConfig | ConvertTo-Json -Depth 20 | Out-File -FilePath $hvConfig.hvConfigPath -Encoding ascii -Force
        Write-Host $script:tick -ForegroundColor Green
        #region Check for ref image - if it's not there, build it
        if (!(Test-Path -Path $newTenant.refImagePath -ErrorAction SilentlyContinue)) {
            Write-Host "Creating reference Autopilot VHDX - this may take some time.." -ForegroundColor Yellow
            New-ClientVHDX -vhdxPath $newTenant.refImagePath -winIso $newTenant.imagePath
        }
        #endregion
    }
    catch {
        $errorMsg = $_
    }
    finally {
        if ($errorMsg) {
            Write-Warning $errorMsg
        }
        else {
            Write-Host $script:tick -ForegroundColor Green
        }
    }
}