Public/New-WindowsServerParentVHD.ps1
function New-WindowsServerParentVHD { [CmdletBinding()] Param( [Parameter (Mandatory=$true)] [ValidateScript({$_ -like "*.vhdx"})] [string]$ParentVHDDestinationPath, [Parameter (Mandatory=$true)] [string]$ISOPath, [Parameter (Mandatory=$true)] [string]$OSEdition, [switch]$Force ) try { if (Test-Path $ParentVHDDestinationPath -ErrorAction SilentlyContinue) { if ($Force -eq $false) { throw "The file '$ParentVHDDestinationPath' already exists." } else { Remove-Item -Path $ParentVHDDestinationPath -Force -ErrorAction 'Stop' } } $FileSystemDrives = Get-PSDrive -PSProvider FileSystem | Select-Object -ExpandProperty Name $MountedISO = $ISOPath | Mount-DiskImage -PassThru try { $MountedISODriveLetter = Get-PSDrive -PSProvider FileSystem | Select-Object -ExpandProperty Name | Where-Object {$_ -notin $FileSystemDrives} Convert-WindowsImage -SourcePath "$MountedISODriveLetter`:\sources\install.wim" -VhdPartitionStyle GPT -VhdPath "$ParentVHDDestinationPath" -Edition $OSEdition } catch { $PSCmdlet.ThrowTerminatingError($_) } finally { $MountedISO | Dismount-DiskImage } Set-ItemProperty -Path $ParentVHDDestinationPath -Name IsReadOnly -Value $true } catch { $PSCmdlet.ThrowTerminatingError($_) } } |