Script to build a VM in Hyper-V and boot from an ISO

This week I was doing a ConfigMgr 2012 R2 class in New York, and one of students asked if he could get the script I used to create the VM I use to build my reference images. Sure, here it is:

$VMLocation = "C:\VMs"
$VMISO = "C:\ISO\MDT Build Lab x86.iso"
$VMNetwork = "Internal"

# Create REF001
$VMName = "REF001"
$VMMemory = 4096MB
$VMDiskSize = 60GB
New-VM -Name $VMName -BootDevice CD -MemoryStartupBytes $VMMemory -SwitchName $VMNetwork -Path $VMLocation -NoVHD -Verbose
New-VHD -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -SizeBytes $VMDiskSize -Verbose
Add-VMHardDiskDrive -VMName $VMName -Path "$VMLocation\$VMName\Virtual Hard Disks\$VMName-Disk1.vhdx" -Verbose
Set-VMProcessor -VMName $VMName -Count 2
Set-VMDvdDrive -VMName $VMName -Path $VMISO -Verbose
Start-VM -VMName $VMName

$VM = Get-VM -Name $VMName
while ($VM.State -ne "off")
{ 
    write-host "The VM is still running" 
    sleep 20 
}

Remove-VM -Name $VMName -Force
Remove-Item -Recurse -Force $VMLocation\$VMName

Happy Deployment,
/ Johan

About the author

Johan Arwidmark

0 0 votes
Article Rating
Subscribe
Notify of
guest
4 Comments
Newest
Oldest Most Voted
Charles
Charles
5 months ago

Is it possible to create convert the ISO to vhdx? so that I can attached the vhdx to the VM and boot from there? its like having a prestage media, but allowing me to change the preferred AdvertID dynamically.

Charles
Charles
5 months ago

Using with the latest version of MECM and I wanted to try creating a prestaged‑media workflow described in the article: https://techcommunity.microsoft.com/blog/coreinfrastructureandsecurityblog/osd-pre-stage-and-uefi-systems/323064 My objective is to: 1. Use MECM to create prestaged media, which outputs a prestage.wim file. 2. Convert that prestage.wim into a bootable VHDX that contains the MECM WinPE boot image. 3. Create a new Hyper‑V VM and attach the VHDX. 4. Boot the VM into WinPE and access the full list of available task sequences, rather than being limited to the single task sequence embedded in the prestaged media. Hence, I could select and deploy any TS with… Read more »


>