MicrosoftWindows

Powershell cmdlets for Hyper-V in Windows Server 8

I had the opportunity to do a little lab time today and I was looking forward to working with Windows Server 8. My test lab is essentially a single Hyper-V server, and a client to access the environment. As such, I wanted to see what I could do in order to speed up my testing.

Long ago I learned that Hyper-V can create differencing disks – i.e. you create a “gold master” image of an OS, and then create one or more differencing disks based on the master image. This prevents you from having to re-install the OS everytime you rebuild your lab, but still required a good deal of clicking to build the VM and VHD appropriately.

Enter PowerShell – the way to automate everything windows. In Server 8, the built-in cmdlets are significantly better, and there is a list of available cmds at http://technet.microsoft.com/library/hh848559.aspx.

I created a base install of Server 8 and sysprep’d it (c:windowssystem32sysprepsysprep.exe). Once the system shutdown, I went into the filesystem of the Hyper-V server and created a “Templates” folder and moved the new vhdx into it. Rename the file to Server8_Template.vhdx and we are all set.

To create a new Server 8 VM I need to perform two steps: 1) Create the differencing hard disk, 2) Create the VM using the harddisk. In my case the VHD’s are on the E: drive of my Hyper-V server HyperTest
Create the differencing disk:

New-vhd “e:vmVirtual Hard DisksDC1.vhdx” -ParentPath e:vmtemplateServer8_template.vhdx -computername HyperTest

Create the VM:

new-vm -Name DC1 -ComputerName HyperTest -memorystartupbytes 1073741824 -VHDPath “e:vmVirtual Hard DisksDC1.vhdx” -switchname Corpnet

My testing is based off the Server 8 Test Labs (http://social.technet.microsoft.com/wiki/contents/articles/7807.windows-server-2012-beta-test-lab-guides-en-us.aspx) and are going to use multiple Server 8 VMs. To automate multiple builds, I created a function that automates all of the necessary steps.

Function MakeServer8VM($vmName){
New-vhd “e:vmVirtual Hard Disks$vmName.vhdx” -ParentPath e:vmtemplateServer8_template.vhdx -computername HyperTest
new-vm -Name $vmName -ComputerName HyperTest -memorystartupbytes 1073741824 -VHDPath “e:vmVirtual Hard Disks$vmName.vhdx” -switchname Corpnet
start-vm $vmName -computername HyperTest
}

The process is exactly the same for Windows 8, just create another template and update the function to use the new template name.

The only item I havnt yet configured is enabling of Dynamic Memory in the VMs. This allows the VM to only claim the memory it needs, and grow/shrink as needed. The new-vm cmdlet doesn’t seem to allow this option, so I am sure it would require an additional step in the function.

One thought on “Powershell cmdlets for Hyper-V in Windows Server 8

  • Anonymous

    I've done the same. Do you have any solution to automatically configure the system first boot after sysprep in powershell ?

Leave a Reply