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("
http://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 (8)

  1. Geetika says:

    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.

  2. aseemb says:

    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.aspx

    Regards

    Aseem Bansal

  3. Geetika says:

    Thanks Aseem. This works. Appreciate it.

  4. Geetika says:

    Hi Aseem,

    I am facing below error while running a test using tcm.exe:

    unable to find a build that matches the build number and definition provided

    Command used:

    $testRunSubmitResult = tcm.exe run /create /title:"Travel FVT test" /planid:1681389 /suiteid:1681484 /configid:192185 /settingsname:"IE11_EN_US" /testenvironment:"Travel – PhantomJS, IE11, Chrome, Firefox" /collection:"xx" /teamproject:"xx" /builddir:"\somebuildpathReleaseTestBinaries" /builddefinition:"Main" /build:"Main_20140915_1810_1124603" /include 2>&1

     Write-Host  $testRunSubmitResult

    I would like to understand what exactly comes in builddir, build and how they are related.

    When I run same test using MTM using run with options (I donn see above build number in the list)

    I tried uding above command with a different build number under same build definition and it gave me error: "A test run must be created with atlease one test case" although I am including /include in my command above.

    The build I chose here doec come in list of builds when I choose to run with MTM using run with options.

    PLease let me know what you think about above error.

    Thanks,

    Geetika

  5. Geetika says:

    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.

  6. aseemb says:

    Good to know that it is working for you, thanks Geetika.

  7. Pradeep says:

    Hi Aseem

    Thanks for your post.

    How to compose a lab environment from standard environment?