How to compose a lab environment from existing SCVMM based machines?

MTM provides various workflows to create environments. You can create environments from virtual machine templates, from stored environments in library, from existing physical/virtual machines which are not managed by SCVMM, from existing virtual machines managed by SCVMM. In this flow, we will learn about the last flow (of creating lab environments from existing virtual machines running on SCVMM hosts) and see the script with which you can achieve this programmatically. Here is how you can use the following script.

  • Login to a machine with tfs object model installed. (The tfs object model gets installed with VS, MTM, Tfs, test controller etc)
  • Open notepad, copy paste the following script and change the highlighted variables as per your setup/deployment.
  • Open a power-shell command prompt and run the modified power-shell script.
  • It should create an environment, provided you have one such machine available in your setup.

Enjoy !!

# Specify the tfs collection url, projectName and test controllerName
$tfsCollectionUrl = New-Object System.URI("
https://myserver:8080/tfs/myCollection");
$projectName = "myProject";
$testControllerName="mycontroller.mydomain.com:6901" #FQDN of the controller

# Load Client Assembly
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Common, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Lab.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);
[Reflection.Assembly]::Load(“Microsoft.TeamFoundation.Lab.Common, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);

# Connect to tfs
$tfsCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsCollectionUrl);
$labService = $tfsCollection.GetService([Microsoft.TeamFoundation.Lab.Client.LabService]);

# Query the host groups
$hostGroupQuerySpec = New-Object Microsoft.TeamFoundation.Lab.Client.TeamProjectHostGroupQuerySpec;
$hostGroupQuerySpec.Project = $projectName;
$hostGroupList = $labService.QueryTeamProjectHostGroups($hostGroupQuerySpec);

# Pick the first host group
$hostGroup = $hostGroupList[0];

# Query the virtual machines
$machineQuerySpec = New-Object Microsoft.TeamFoundation.Lab.Client.VirtualMachineQuerySpec;
$machineQuerySpec.Location = $hostGroup.Uri;
$machineQuerySpec.FilterAlreadyImportedVirtualMachines = $TRUE;
$virtualMachines = $labService.QueryVirtualMachines($machineQuerySpec);

# Pick the first virtual machine
$virtualMachine = $virtualMachines[0];

# Create lab system definition
$lsName = "machine1";
$roleName = "webRole";
$labSystemDefinition = New-Object Microsoft.TeamFoundation.Lab.Client.LabSystemDefinition($lsName,$lsName,$roleName);
$labSystemDefinition.SourceIdentifier = $virtualMachine.VMGuid.ToString();
$labSystemDefinition.SourceType = [Microsoft.TeamFoundation.Lab.Client.LabSystemSourceType]::VirtualMachine;
$labSystemDefinition.ConfigurationComplete = $TRUE;

# Create lab environment definition
$leName = "aseemEnvironment";
$leDescription = "This is Aseem's environment";
$labSystemDefinitions = [array]$labSystemDefinition
$labEnvironmentDefinition = New-Object Microsoft.TeamFoundation.Lab.Client.LabEnvironmentDefinition -ArgumentList @(,$leName, $leDescription, $labSystemDefinitions);
$labEnvironmentDefinition.TestControllerName = $testControllerName;

# Create the environment
$hostGroup.CreateLabEnvironment($labEnvironmentDefinition, $NULL, $NULL);