This article is more than 1 year old
Want to deploy virtual machines in a hurry? PowerShell is your friend
Improve your automation
Gold standard
Some sysadmins I have come across believe that the golden image should be kept simple and contain no more than the operating system plus updates and administrative account.
I like simple and use my vanilla virtual machines as often my customised ones, but outside a test lab it doesn’t make a lot of sense to me to rapidly deploy a virtual machine and then have to go about installing everything I need.
My test labs reflect my real world environments so the virtual machines I rapidly deploy are generally the same as those I need to deploy in production.
If your test lab storage is especially constrained then you might like to keep only vanilla golden images and the use PowerShell to add roles – or additional software – later. It just comes down to what works in your environment.
I have a new payload folder I transfer to every virtual machine as one of the first steps in my configuration process. It contains, among other things, a number of PowerShell script (.PS1) files.
Although you can run the sysprep command manually, either from a batch file or from the CMD prompt, I like to keep everything PowerShell wherever I can.
The PowerShell commands to achieve this is:
Start-Process -FilePath C:\Windows\System32\Sysprep\Sysprep.exe -ArgumentList '/generalize /oobe /shutdown /quiet'
If your virtual machine is using dynamically expanding VHDX files then once you have finalised the sysprep stage of your golden image creation it is a good idea to run a shrink command across the VHDX file. The code to achieve this is extremely simple and one of the last manual steps you will run:
Param([string]$Path = $(Throw '-Path is required')) Echo "Attempting to Mount $Path" Mount-vhd -path $Path -readonly Optimize-vhd -path $Path -mode full Echo "Attempting to dismount $Path" Dismount-vhd -path $Path
Once that is completed you can begin the next phase. From here you will need only the .vhdx virtual drive file, so you can either keep the virtual machine or delete it out of the Hyper-V Management Console once you are certain you are done. If you are keeping it, make sure you shut it down.