Partager via


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);

Comments

  • Anonymous
    June 04, 2014
    Hi Aseem,Thanks for your post.I am looking for how to find if an environment is healthy using powershell script.HOw to get agents and see if all agents are online and in Ready state.Thanks.

  • Anonymous
    June 04, 2014
    Thanks Geetika for your question. See whether the script in the below link meets your requirement or not.blogs.msdn.com/.../how-to-find-whether-lab-environment-is-in-healthy-state-or-not.aspxRegardsAseem Bansal

  • Anonymous
    June 05, 2014
    Thanks Aseem. This works. Appreciate it.

  • Anonymous
    September 18, 2014
    The comment has been removed

  • Anonymous
    September 18, 2014
    I found the issue. The build number that I was using had a failed build status. For that I need to update my script to get build number status and run test against only Successful builds.

  • Anonymous
    September 18, 2014
    Good to know that it is working for you, thanks Geetika.

  • Anonymous
    February 18, 2016
    Hi Aseem Thanks for your post. How to compose a lab environment from standard environment?

  • Anonymous
    February 21, 2016
    Refer this blogs.msdn.microsoft.com/.../create-lab-environment-using-tfs-lab-management-apis