Last active
December 16, 2024 11:09
-
-
Save HighLibrarian/3f0a1f06ba95e78f61aa5e1afd09c2e0 to your computer and use it in GitHub Desktop.
New-DifferencingVm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-DifferencingVm | |
{ | |
param ( | |
[Parameter(Mandatory=$true)] | |
[string]$VMName, | |
[Parameter(Mandatory=$false)] | |
[string]$DiskName=$VMName, | |
[Parameter(Mandatory=$false)] | |
[ValidateSet('Windows11-x64-24h2-BusinessEditions-112024.vhdx')] | |
[string]$BaseDiskName, | |
[Parameter(Mandatory=$true)] | |
[ValidateSet('light', 'medium', 'heavy', 'custom')] | |
[string]$Memory, | |
[Parameter(Mandatory=$true)] | |
[ValidateSet('Default Switch','LAB-INTERNAL', 'LAB-EXTERNAL')] | |
[string]$vSwitch, | |
[parameter (Mandatory = $true)] | |
[switch]$StartVm | |
) | |
# Config | |
$HPVBaseLocation= Get-VMHost | Select-Object VirtualHardDiskPath -ExpandProperty VirtualHardDiskPath | |
$VmLocation = "$HPVBaseLocation\" | |
$BaseDiskLocation = "$HPVBaseLocation\PARENTS\Virtual HardDrives" | |
# get basedisks | |
if ($null -eq $BaseDiskName) | |
{ | |
$BaseDiskName = Get-ChildItem -Path $BaseDiskLocation -Filter "*vhdx" | Select-Object name -ExpandProperty name | Out-GridView -PassThru -Title "Select the Parent Disk for $vmname" | |
} | |
$VMConfig =@{ | |
path = "$VmLocation" | |
name = $VMName | |
NoVHD = $true | |
generation = 2 | |
switch = $vSwitch | |
} | |
$VHDConfig =@{ | |
parentPath = "$BaseDiskLocation\$basediskname" | |
path = "$VmLocation\$VMName\Virtual Hard Disks\$vmname.vhdx" | |
} | |
#Create a differencing disk based on our base disk | |
New-VHD @VHDConfig -Differencing -Verbose | |
#Create our VM | |
New-VM @vmconfig | |
#Add the disk to our vm | |
Add-VMHardDiskDrive -Path $($VHDConfig.path) -VMName $VMName -Verbose | |
#config our firmware | |
Set-VMFirmware -VMName $VMName -EnableSecureBoot 'on' -FirstBootDevice (Get-VMHardDiskDrive -VMName $VMName) | |
switch ($Memory) | |
{ | |
"light" | |
{ | |
Set-VMMemory -VMName $VMName -MinimumBytes 1Gb -StartupBytes 2Gb -MaximumBytes 4Gb | |
} | |
"medium" | |
{ | |
Set-VMMemory -VMName $VMName -MinimumBytes 2Gb -StartupBytes 4Gb -MaximumBytes 8Gb | |
} | |
"heavy" | |
{ | |
Set-VMMemory -VMName $VMName -MinimumBytes 4Gb -StartupBytes 8Gb -MaximumBytes 6Gb | |
} | |
Default {} | |
} | |
if ($StartVm ){Start-VM -Name $VMName -Verbose} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment