Category: SCVMM

TFS Lab Build needs to start the environment

Most of the Team Foundation Server virtual lab environments I work with use snapshots that are taken while the lab virtual machines are running. When configuring a Build Definition with the default lab build process template (LabDefaultTemplate.xaml) and choosing to restore the environment to one of these snapshots everything just works. However, the guidance from Microsoft around taking snapshots of certain virtual machines (primarily Active Directory domain controllers and occasionally SQL Servers) is to shutdown the VMs first before taking the snapshot.

In recent work with a lab containing a domain controller, we’ve followed the recommendation of performing the snapshot while the VM is off. As a result of this I’ve discovered that the default lab build process template will restore to the selected snapshot and wait 20 minutes for the workflow capability of the environment to become ready and then fail. It never checks if the environment is started and never attempts to start it.

I’ve since added a call to the GetLabEnvironmentStatus and StartLabEnvironment activities into the lab build process template just after the “Restore Snapshot” sequence and before the “Do deployment” sequence so the environment gets started if it isn’t already running.

The full customised build process template is available as a Gist. The changes are small though – first a new variable definition is inserted at line 30:

<Variable x:TypeArguments="mtlc:LabEnvironmentState" Name="LabEnvironmentState" />

And then 6 lines are inserted at line 81 (was line 80 before the above line was added):

<mtlwa:GetLabEnvironmentStatus LabEnvironmentUri="[LabEnvironmentUri]" Result="[LabEnvironmentState]" DisplayName="Get Lab Environment State" />
<If Condition="[LabEnvironmentState &lt;&gt; Microsoft.TeamFoundation.Lab.Client.LabEnvironmentState.Running]" DisplayName="If Lab Environment Not Running">
  <If.Then>
    <mtlwa:StartLabEnvironment LabEnvironmentUri="[LabEnvironmentUri]" DisplayName="Start Lab Environment"/>
  </If.Then>
</If>

A very similar change will be required in TFS 11 because its lab build process still won’t start an environment. Apparently this is to avoid making assumptions about the order in which each VM in a lab should be started.

Create SCVMM Templates with PowerShell

Now that I get to work with a TFS 2010 Lab Management environment most days, I find myself building various virtual machines to replicate the  production environments of our clients for testing. With many different clients and projects, the range of virtual machine operating systems expands exponentially as matrix of core OS version, processor architecture, service pack, IE version, and other minor variations. However, for any particular configuration, I’ll also want multiple copies so naturally I want to make use of System Center Virtual Machine Manager’s VM Template Library.

However, creating a template from a VM using the SCVMM Administrator Console, without destroying the original VM, is death by a thousand clicks:

  1. Shutdown the VM.
  2. Dismount any media in the VM’s virtual DVD drives.
  3. Clone the VM via an eight-page wizard.
  4. Wait for the cloning to complete.
  5. Convert the cloned VM into a template with a six-page wizard.
  6. Wait for the sysprepping to complete.
  7. Restart the original VM.

I tire of such tedium very quickly and as such, I’ve scripted the above process with PowerShell and the SCVMM Snapin. You can access the Export-VMTemplate script I’ve written on gist.github. If you open a PowerShell window from the toolbar in the SCVMM console, you can execute the script like this:

.\Export-VMTemplate.ps1 -VM 'MyOriginalVm' -TemplateName 'NewTemplateName' -LibraryServer 'MyLibSvr'

Hopefully the script itself, or at least some of the concepts within, will be useful to someone else.