Do{IT}urself

THE INFRAITHOBBIEGEEKBLOG

By

Powershell commands to create Hyper-v 2012 VMs

In this post I wanted to point some interesting way to create VMs.

A VM can host several type of virtual disks

First there is two file format VHD and the new VHDX. VHD can be up to 2TB where VHDX can go up to 64TB. Also VHDX are more resilient to VM hard shutdown but are only supported by WS 2012 and so I think also Windows 8.

Then there is three types of disks :

  • Fixed : The complete volume of the disk is created at the beginning. This kind of disk is more effective but will use the total allocated space.
  • Dynamic : this type of virtual disk create a small disk that contain the data hosted. Then as long as you fullfill the disk in the VM the hosted file will grow
  • Differencing : this type of disk is based on another disk called the parent disk. The child disk contain only the differences you made in the VM. Both disks (parent and child) could be Fixed or Dynamic disk but the format should be the same (VHD or VHDX). This kind of disk reduce disk space usage.

If you use the UI or the command line New-VM the default dynamic type will be used.If you want to use fixed or differencing size you’ll need to create the disk separatly.

Of course you can use the UI to create VHD and VM, it’s great to build a few VM but if you want to create several VM and always use the same configuration you really should write some Powershell Scripts.

Here are the Command to create all sort of disks :

Dynamic without source :

New-VHD -Path “C:ClusterStorageVolume1BASE2012.VHDX” -Dynamic -SizeBytes 127GB -ComputerName hyperv01

Dynamic with source :

New-VHD -Path “C:ClusterStorageVolume1VM001.VHDX” -ParentPath “C:ClusterStorageVolume1BASE2012.VHDX” -Dynamic -SizeBytes 127GB -ComputerName hyperv01

Fixed without source :

New-VHD -Path “C:ClusterStorageVolume1BASE2012.VHDX” -Fixed -SizeBytes 60GB -ComputerName hyperv01

Fixed with source :

New-VHD -Path “C:ClusterStorageVolume1VM001.VHDX” -SourceDisk C:ClusterStorageVolume1BASE2012.VHDX -Fixed -SizeBytes 60GB -ComputerName hyperv01

Differencing disk :

New-VHD -Path “C:ClusterStorageVolume1VM001.VHDX” -ParentPath “C:ClusterStorageVolume1BASE2012.VHDX” -Differencing -SizeBytes 127GB -ComputerName hyperv01

(The source is a base disk)

To create a new VM you can :

Create a VM and a new VHD (Dynamic) :

New-VM -Name VM01 -NewVHDPath “c:ClusterStorageVolume1VM01.VHDX” -NewVHDSizeBytes 60GB -ComputerName Hyperv01

Create a VM with no disk :

New-VM -Name VM01 -noVHD -ComputerName Hyperv01

Create a VM and attach an existing VHD :

New-VM -Name VM01 -VHDPath “c:ClusterStorageVolume1VM01.VHDX” -ComputerName Hyperv01

Ok so now that we have all the basics command let’s create a script to build 5 VM with fixed size disk (CreateVMFixedVhd) :

$vhdpath = “C:ClusterStorageVolume1VHD”
$vmnetworkName = “External01″
$memorySize = 1GB
$VmtoCreate = 5
$DiskSize = 60GB
$HyperVhost = hyperv01

1..$VmtoCreate | % {
New-VHD -Path $vhdpath”VM$_.VHDX” -Fixed -SizeBytes $DiskSize -ComputerName $HyperVhost
New-VM -Name VM00$_ -VHDPath $vhdpath”VM$_.VHDX” -Memory $memorySize -SwitchName $vmnetworkName -ComputerName $HyperVhost
Start-VM VM00$_ -ComputerName $HyperVhost

}

The problem with this method is that you’ll have to install all OS. Thus, we’ll need a source disk.To do so we will first create the base VM to create the base image :

New-VHD -Path “C:ClusterStorageVolume1VHDBASE2012.VHDX” -Fixed -SizeBytes 60GB -ComputerName hyperv01

New-VM -Name VMBase2012 -VHDPath “C:ClusterStorageVolume1VHDBASE2012.VHDX” -ComputerName Hyperv01

Set-VMDVDDrive -VMName VMBase2012 -Path C:ClusterStorageVolume1ISOWin2012_RTM.iso -CmputerName Hyperv01

Start-VM VMBase2012 -ComputerName Hyperv01

  • Install the OS
  • Install integration services and make any changes you want
  • Sysprep it and shutdown
  • c:WindowsSystem32sysprepsysprep.exe /generalize /oobe /shutdown
  • Remove the base2012 VM from Hyperv manager (Command : Remove-VM Base2012)
  • This command delete the VM but not the VHD

Then we’ll be able to use this disk as a source for our new virtual machines.

Indeed, I choose to build several VM using Differencing disks.

$vhdpath = “C:ClusterStorageVolume1VHD”
$vmnetworkName = “External01″
$memorySize = 1GB
$VmtoCreate = 5
$DiskSize = 80GB
$HypervHost = “hyperv01″
1..$VmtoCreate | % {
New-VHD -Path $vhdpath”VM00$_.VHDX” -ParentPath $vhdpath”BASE2012.VHDX” -Differencing -SizeBytes $DiskSize -ComputerName $HypervHost
New-VM -Name VM00$_ -VHDPath $vhdpath”VM00$_.VHDX” -Memory $memorySize -SwitchName $vmnetworkName -ComputerName $HypervHost
Start-VM VM00$_ -ComputerName $HypervHost
}

Then if you want to add this Virtual machines to your failover cluster

$VmtoAdd = 5
$ClusterName = “Cluster01″
1..$VmtoAdd | % {
Add-ClusterVirtualMachineRole -VirtualMachine VM00$_ -Name VM00$_ -Cluster $ClusterName
}

Enjoy

Source :

http://technet.microsoft.com/en-us/library/hh848559.aspx

http://technet.microsoft.com/library/hh847239.aspx

One Response to Powershell commands to create Hyper-v 2012 VMs

  1. Jamal says:

    I was pulling my hair on why my VM is keep coming up as 4096 KB… Then found it is dynamic disk. Saw max capable size of my desired size. Time for coffee now……

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>