Recently I had a project for which I needed about 300 virtual machines. I already had a bare metal with Quad Processors and 512GB Ram loaded with Windows 2012 R2 and Hyper-V. I was sure it would handle at least 100 VMs easily with 4GB ram each. Now the difficult task to create these big number of virtual machines which was way too time consuming and boring task.
So my next step was to Google and find out how I could create bulk VMs on Hyper-V. As per my knowledge, Hyper-V doesn't have any option to create VMs in bulk quantity. So I had to go with PowerShell. PowerShell was already installed on my bare metal. I will try to explain what I did in steps below.
Step 1 - Obtain Cmdlets - PowerShell Management Library for Hyper-V
Cmdlets is a library of few commands specific for Hyper-V and helps creating and managing virtual machines through PowerShell. You can download CMDlets from Codeplex website
. You can learn about its commands Microsoft technet website
Step 2 - Download batch VM create PowerShell script
Microsoft technet blog has a very good ready to use script for creating bulk virtual machines. This helped me the most. Download it here
. However it does not give you the CSV file which you have to use to create bulk virtual machines. For your help, I will attach the CSV file same below. You also have to prepare a virtual machine manually via Hyper-V first which will be used as a template.
Step 3- Create template virtual machine
So I logged into Hyper-V and created the first virtual machine and named it as "Template Win 2008 R2". Then I logged into the virtual machine, brought it on public internet using the additional IP address and using Google DNS server. Once the desktop is loaded, I installed the necessary software on it. Like Google Chrome browser, Winrar or whatever I needed to use.
Important things to remember while creating the Template VM
- Make sure the Windows on your VM is licensed and fully activated otherwise you will have to manually enter the License key in all newly created VMs one by one.
- Make sure none of your installed software has start up calls in Windows. Like if you install Download Accelerator Plus, it would auto start with your windows. Likewise many programs do this. So you must disable this from Start > Run > msconfig and then go to Startup tab and uncheck all unnecessary values.
- Make sure Windows Auto Update is turned off otherwise the VMs will auto restart closing any running software on it without your permission or knowledge.
- This is a template VM. So how you set this up, all your new VMs will have everything exactly like this. With all software and settings.
- After full setup, make sure your Windows is shut down properly.
Now your template VM is fully ready and shut down mode, the PowerShell script will be able to copy its hard disk (example.vhdx) for new VMs.
Step 4 - Prepare the CSV file
Now we have to prepare our CSV file with a list of VMs we want to create. You can download the sample CSV file
here. I will explain a little bit about each column in the CSV file.
- VHDFolder - This is the path where you store all your hard drives for the virtual machines.
- VHDName - The name for each VM's hard drive. I used numbers with a letter so I can refer them correctly in future.
- VHDParentPath - This is where you define the full path of your Template VM's hard drive. It will be copied for your new VMs and you will see everything exactly like the template VM you set up.
- VMFolder - This is the folder in which I store all my VMs' configuration files. Each VM creates its own folder under in this path.
- VMName - The name of the VM, I use numbers as like hard drive names for easy reference.
- MemoryStartupBytes - I created each VM with 4GB ram. You have to define it in MB value
- VMSwitchName - Virtual switch name is required if you want your VMs to use public internet inside it. This switch has to be created in your Hyper-V settings
Step 5 - Run script in PowerShell
Next step is to open PowerShell in your Windows 2012 R2. Type or copy the following command.
Change the path to the script and make sure it is unlocked to execute in PowerShell.
Once you run the script, you should be able to see the PowerShell creating the VMs for you and will be showing you output with results like below.
I hope the above tutorial helps and works smooth. Please feel free to leave the comment below with your feedback or questions.
Sources: Image credits and tutorial is referenced from Microsoft Script Center