This script is used to "zero out" the development VM, update the DevDisk.vhdx image and then restart the VM, booting to the DevDisk.vhdx image. This is the one I use most often while iterating through development changes using the VM. The script takes a couple of parameters:

  • -UpdateMedia
    Instructs MDT to update the \DeploymentMedia1 folder, which forces an update to the WIM file used for image captures, among other things. This can take 20-30 minutes.
  • -UpdateBoot
    Instructs MDT to update the boot image for the main \DeploymentShare.ROOT share, which is used as the content for the MDTBOOT partition. This can take a bit, depending on what has changed.

The VM, if running will be stopped and its internal disk (disk 0) will be wiped completely (no partitions). The DevDisk.vhdx disk will be mounted and updated (after the actions of the UpdateMedia and UpdateBoot tasks, if specified). Then the VM will be launched and off it'll go. Mostly, this script just stops the VM and clears the main VHDX file before calling out over to the UpdateUSBDrive.ps1 script with the -UpdateDevDisk parameter plus either of the UpdateMedia or UpdateBoot options.

Param([switch]$UpdateMedia,[switch]$UpdateBoot)

Write-Host "Stopping any VMCONNECT sessions"
Get-Process -Name vmconnect -ErrorAction SilentlyContinue | Stop-Process -Force -Confirm:$false

If ((Get-VM -Name MDT01).State -ne 'Off') {
    Write-Host "Stopping the virtual machine"
    Stop-VM -Name MDT01 -Force -Confirm:$false
}

Write-Host "Mounting main disk"
$A = Mount-VHD -Path D:\VMs\MDT01.vhdx -NoDriveLetter -PassThru
Write-Host "Clearing partitions"
Clear-Disk -Number ($A | Get-Disk).Number -RemoveData -RemoveOEM -Confirm:$false
Write-Host "Dismounting main disk"
$A | Dismount-VHD

Write-Host "Gathering disks"
$MainDisk = Get-VMHardDiskDrive -VMName MDT01 -ControllerLocation 0
$DevDisk = Get-VMHardDiskDrive -VMName MDT01 -ControllerLocation 1

Write-Host "Resetting boot order"
Set-VMFirmware -VMName MDT01 -BootOrder @($MainDisk,$DevDisk)
if ($UpdateMedia) {
    if ($UpdateBoot) {
        .\UpdateUSBDrive.ps1 -UpdateMedia -UpdateBoot -UpdateDevDisk
    } else {
        .\UpdateUSBDrive.ps1 -UpdateMedia -UpdateDevDisk
    }
} else {
    if ($UpdateBoot) {
        .\UpdateUSBDrive.ps1 -UpdateBoot -UpdateDevDisk
    } else {
        .\UpdateUSBDrive.ps1 -UpdateDevDisk
    }
}