Skip to content

soheilamiri/AzureHCI

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 

Repository files navigation

CreateHypervVms is to create Windows VMs on Hyper-V using PowerShell

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.

Why? Use cases:

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.

What it does - use cases

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.

Credits & Inspirations - Or Why I Did This?

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:

  1. For learning & understanding
  2. For having something that is packageable - lightweight - customizable - from the creation to the final OS.

Requirements

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.

How to use it.

CreateHypervVMs on YTube

  1. Choose a starting repository e.g. Scenario-Blueprint,....
  2. Copy the selected repository's content to your Hyper-V system - e.g. c:\temp
  3. 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.
  1. 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
  1. 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
  1. 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)
  1. 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.
  2. 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.
  3. 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.

About

deploy Hypercover infrastructure

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PowerShell 100.0%