Tuesday, November 22, 2011

Previously I have written about formatting MAC addresses from Hyper-V WMI into a format to be consumed by another program and about duplicating a Master (or Template) VM into multiple virtual machines.
Today I am going to put that together and produce an output that contains the each VM as a name, mac address pair in an array.
My Master / Template VM has the MAC address set to dynamic, so this property is going to repeat. I am also going to take advantage on the Hyper-V process of booting the VM to assign the MAC address.
From my previous duplication script I have an array of VMs: $arrNewVms

In this I refer to a helper Function “ProcessWMIJob” this is handy in that it forces me to wait until the Start VM process is completed. If I didn’t use this my VMs would not be started and therefore will not in turn power off – it becomes a bit of a timing issue.
The Function can be found at the beginning of the script here.
Next time, something completely different.

This is actually a highly useful bit of code if you want to make bunches of VMs that are identical in settings. You create the first “MasterVM” – you could also call a template. Then copy it.

I did tweak Taylor’s PowerShell a bit, but only to remove an error that annoyed me with Write-Progress and to pre-pend and post-pend the VM names, the count to start at and the number to create.

I will add more to this later as this is just the beginning. After all of this creation of VMs there are other settings that can be applied and considerations that you must have in regards to the system as a whole.

What this script does is use the Hyper-V Export and Import feature. The Export creates a copy of the VM (and all of its associated physical objects) in a nice tidy folder in the path that is specified.

This target can be local storage or if your target Hyper-V server is a node of a cluster it can be a CSV. And the plus of using Export to copy is that the VM is all set to be Highly Available.

The Import takes advantage of Hyper-V doing all the work to generate the new virtual objects, apply security settings, modify any snapshots files, handle differencing disks, etc. Let the system do all that work so I don’t have to. After all, someone already went to all the trouble to build the method, and it gets tested.

Without further delay, here is my version of the script.

param ( [string]$masterVm = $(Throw "MasterVM required"), [string]$exportPath = $(Throw "Path required to the Hyper-V local storage where VMs will be created"), [string]$namePrePend = $(Throw "A string to add to the front of the new vm name"), [string]$namePostPend = $(Throw "A string to add to the end of the new vm name"), [string]$hypervHost = $(Throw "The Hyper-V host where the template VM is registered and where VMs will be created"), [int] $startAt = $(Throw "The number to begin the VM creation with (ie. 2 = start with VM 2)"), [int]$numCopies = $(Throw "The number of VMs to create")

)

function ProcessWMIJob { # Is there a way to loop through this and have these process and manage multiple jobs asynchronously? The problem might be in overloading the storage layer. param ( [System.Management.ManagementBaseObject]$Result )

That is all fine and dandy for the example. But I tried it on a VM that is powered off, and you know what? I got nothing back. A NULL came back from WMI.

I searched for quite a long time trying to figure out what I was doing wrong.

In the end, I discovered I was doing nothing wrong. The VM has to be running for this association to return the vNIC.

I actually find this interesting. As once the object is created, it exists, and it has properties. I already powered the VM on and off for the MAC address to be automatically assigned. And then I can’t query it. It makes little sense to me. Maybe I can get Ben to respond and fill in that lack in my understanding.

In the end I simply modified my script. I have to briefly power on the VM and then off again to automatically assign the MAC, so why not take advantage of that.

Subscribe

Me.

I have been a full-time IT professional since 1996. Keeping networks, telecom, systems, and core business applications alive and well. In 2006 I joined the ranks of the software vendor. Now, I figure out how to improve and advance these same systems, in the process I learn a lot about them.
MVP, CCEA, VCP, MCTS, N+