Private/New-ClientVHDX.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 |
#requires -Modules "Hyper-ConvertImage" function New-ClientVHDX { [cmdletbinding(SupportsShouldProcess)] param ( [Parameter(Position = 1, Mandatory = $true)] [string]$vhdxPath, [Parameter(Position = 2, Mandatory = $true)] [string]$winIso, [Parameter(Position = 3, Mandatory = $false)] [switch]$unattend ) try { $module = Get-Module -ListAvailable -Name 'Hyper-ConvertImage' if ($module.count -lt 1) { Install-Module -Name 'Hyper-ConvertImage' $module = Get-Module -ListAvailable -Name 'Hyper-ConvertImage' } if ($PSVersionTable.PSVersion.Major -eq 7) { Import-Module -Name (Split-Path $module.ModuleBase -Parent) -UseWindowsPowerShell -ErrorAction SilentlyContinue 3>$null } else { Import-Module -Name 'Hyper-ConvertImage' } $currVol = Get-Volume Mount-DiskImage -ImagePath $winIso | Out-Null $dl = (Get-Volume | Where-Object { $_.DriveLetter -notin $currVol.DriveLetter}).DriveLetter $imageIndex = Get-ImageIndexFromWim -wimPath "$dl`:\sources\install.wim" Dismount-DiskImage -ImagePath $winIso | Out-Null $params = @{ SourcePath = $winIso Edition = $imageIndex VhdType = "Dynamic" VhdFormat = "VHDX" VhdPath = $vhdxPath DiskLayout = "UEFI" SizeBytes = 127gb } if ($unattend) { $params.UnattendPath = $unattend } Write-Host "Building reference image.." -ForegroundColor Cyan -NoNewline Convert-WindowsImage @params } catch { Write-Warning $_ } finally { if ($PSVersionTable.PSVersion.Major -eq 7) { Remove-Module -Name 'Hyper-ConvertImage' -Force } } } |