Warning: Use at you own risk. No liabilities. Although the script checks i.e. should not overwrite stuff I don't guarantee. Make sure you have a backup.
You like PowerShell and are using Hyper-V and maybe need to:...
- ...create a lot of VMs.
- ...have a re-playable solution (e.g. Lab / Demo / Education environments)
- ...do a zero touch deployment and require automation
- ...bake your knowledge into script not screenshots.
--> then you should continue reading.
This set of scripts will create VMs on Hyper-V based on a golden image (i.e. sysprepped .vhdx) with the specific settings that you predefine in PShell config files.
- ...VMs will be created with the size, name, # of network adapters, # disks on Hyper-V how you define 1_VM.psd1
- ...Each VM gets its own configuration using unattend.xml - how you specify 2_UnattendSettings.psd1
- ...After the OS is installed you can define a set of post install PShell scripts to be run to custimize each individual VM 3_PostInstallScripts.psd1
-->take Scenario-Blueprint as starting point and adjust to your needs.
I have been working with Hyper-V for years and created a lot of VMs over the time - either manual or automatically. It was blog posts like these:
- https://github.com/microsoft/MSLab
- https://github.com/BladeFireLight/WindowsImageTools/tree/master
- https://learn.microsoft.com/en-us/archive/blogs/virtual_pc_guy/script-image-factory-for-hyper-v
- http://www.altaro.com/hyper-v/creating-generation-2-disk-powershell/ that inspired me to code this. So why did I still do this? Well for 2 reasons:
- For learning & understanding
- For having something that is packageable - lightweight - customizable - from the creation to the final OS.
OS: Windows Server 2016, Windows Server 2019, Windows Server 2022, Azure Stack HCI (22H2, 23H2) Components: Hyper-V with corresponding PowerShell module.
Works either launched locally or using PS remoting.
- Choose a starting repository e.g. Scenario-Blueprint,....
- Copy the selected repository's content to your Hyper-V system - e.g. c:\temp
- Create a sysprep'ed .vhdx containing the Windows OS you want using e.g. CreateVhdxFromIso.ps1.
.\CreateVhdxFromIso.ps1 -IsoPath "D:\iso\Windows Server 2022\SERVER_EVAL_x64FRE_en-us.iso" -VhdxPath 'c:\images\W2k22.vhdx' -SizeBytes 80GB -ImageIndex 4
# you can use .iso files with OS from Microsoft.
# -ImageIndex 1 : is the first image in the install.wim
# -ImageIndex 1 : mostly is a Server Standard Core
# -ImageIndex 4 : mostly is a Server Datacenter with GUI
# Mount .iso and run Get-WindowsImage -ImagePath "X:\sources\install.wim" to find out what imageindex to choose.
- Adjust the $GoldenImage variable in your copy of the e.g. CreateHypervVms.ps1 file to match the path from step 2.
# 1. Create a golden image and adjust these variables
$GoldenImage = "c:\images\W2k22.vhdx" # path to a sysprepped virtual hard disk (UEFI i.e. Gen2 VMs) to be used as a golden image
- Make the $vmDirectoryPrefix variable of your copy of CreateHypervVms.ps1 point to the VMs final path destination.
$vmDirectoryPrefix = "c:\ClusterStorage\CSV1\createvms" # generic path where the VMs will be created - each VM gets its subfolder
- Choose a complex default password for your $adminPassword variable of your copy of CreateHypervVms.ps1
# Provide a complex generic local admin pwd
$adminPassword = 'some0815pwd!' # use single quotes to avoid PS special chars interpretation problems (e.g. $ in pwd problems)
- Define the Hyper-V details of the VMs to create by modifying your copy of 1_VMs.psd1. Open the file in a PowerShell scripting editor and read the comments.
- Define the OS details of the VMs by modifying your copy of 2_UnattendSettings.psd1. Open the file in a PowerShell scripting editor and read the comments.
- Define the Post installation steps of the VMs by modifying your copy of 3_PostInstallScripts.psd1. Open the file in a PowerShell scripting editor and read the comments.