Hyper-V and the need for speed…

Sometimes I need to build a new Windows image  to deploy with SCCM at work.
And it would be nice if it didn´t took too long time to build, because sometimes you need to test new things and the image might fail while building it and you have to correct the fault and start over again.

At work I have a Hyper-V host with plenty of ram, so why not make a ramdisk to have the VM-disks on?
But the backside is that when you reboot the host, the disk-files is lost and you manually need to remove and add new disks to the VM….or do you?

My setup is:

  • MDT and ADK hosted on another server
  • Hyper-V host with Windows Server 2012 R2 and 192Gb ram
  • A 80Gb ramdrive (enough for 2*40Gb virtual disk) made with ImDrive software
  • Powershell of course!

My VMs are stored on a ordinary disk and have these specification:

  • Generation 1 VM
  • 4Gb ram
  • 1 cpu, 2 cores
  • No HDD!
  • 1 Network adapter, legacy adapter is not necessary in my case. I boot from an ISO-files from the MDT-share directly.
  • A static MAC-adress
  • A snapshot when everything is configured

So, my idea is to script a sequence where I create a virtual disk on the ramdrive -> connect it to the VM -> start VM and buid image -> disconnect disk when finished -> delete disk file.

This script will do that. It starts the VM and waits for it to shutdown and then removes the disks and disk-files and finaly reverts the snapshot.

$VMName = "Windows10x64BC"
$DiskFileName = "Disk0.vhdx"
$VMDiskFolder = "R:\Hyper-V" # Location to ramdisk-drive and a subfolder. will be created by hyper-v if not existing
$VMDiskPath = "$VMDiskFolder\$VMName\$DiskFileName" #Folder doesnt need to exist
$DiskSize = "40960000000" # 40Gb in Bytes!

#Removes old drive
Function RemoveAllDrives {
    Get-VMHardDiskDrive -VMName $VMName | ForEach-Object {
        Write-Host "Found Drive $($_.Path)"
        Write-Host "Removing drive from VM"
        Remove-VMHardDiskDrive -VMName $VMName -ControllerNumber $($_.ControllerNumber) -ControllerLocation $($_.ControllerLocation) -ControllerType $($_.ControllerType)
        Write-Host "Deleting Drive-file from disk"
        Remove-Item -LiteralPath $($_.Path) -Force
    }
    Remove-Item -LiteralPath $VMDiskPath -Recurse -Force
    Remove-Item -LiteralPath "$VMDiskFolder\$VMName" -Recurse -Force
}
RemoveAllDrives
#Adds new drive
New-VHD -Path $VMDiskPath -SizeBytes $DiskSize
Add-VMHardDiskDrive -VMName $VMName -ControllerType IDE -Path $VMDiskPath

#Run and wait for VM to finish, then revert to snapshot
 
# Start VM
Start-VM -VMName $VMName

# Monitor VM to see if it's running
$VM = Get-VM -Name $VMName
while ($VM.State -ne "off")
{
    write-host "The VM is still running"
    sleep 20
}
 
# Restore VMSnapshot
RemoveAllDrives
Get-VM –Name $VMName | Get-VMSnapshot  | Sort CreationTime | Select -Last 1 | Restore-VMSnapshot -Confirm:$false

Happy image-building!
 

Leave a Reply

Your email address will not be published. Required fields are marked *