How to create standard environment programmatically?

MTM provides a good UI flow to create the standard environments but one of the external customer wanted to create such environments programmatically. Here is the script to do that and the steps you should perform to run this 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 a standard environment

Enjoy !!

# Define parameters
$tfsCollectionUrl = New-Object System.URI("
$projectName = "myproject";
$environmentName = "myEnvironment";
$testControllerName = "mycontrollerName:6901";

$machine1 = "";
$machine1Role = "TestMachine";

$domainName = "mydomain";
$userName = "myuser";
$password = "mypassword";

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

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

# Create the machine definition object
$machine1D = New-Object Microsoft.TeamFoundation.Lab.Client.LabSystemDefinition ($machine1, $machine1, $machine1Role);
$machine1D.SourceType = [Microsoft.TeamFoundation.Lab.Client.LabSystemSourceType]::TestMachineLocator;
$machine1D.ConfigurationComplete = $true;

$machinesD = New-Object System.Collections.Generic.List[Microsoft.TeamFoundation.Lab.Client.LabSystemDefinition];

# Create the environment definition object
$environmentD = New-Object Microsoft.TeamFoundation.Lab.Client.LabEnvironmentDefinition ($environmentName, $null, $machinesD);
$environmentD.TestControllerName = $testControllerName;

# Create the environment
$environment = $labService.CreateLabEnvironment($projectName, $environmentD, $null, $null);

# Install the test agent on the box.

$securePassword = ConvertTo-SecureString $password  -AsPlainText -force
$adminInfo = New-Object Microsoft.TeamFoundation.Lab.Client.AccountInformation($domainName, $userName, $securePassword)
foreach ($vm in $environment)
   Write-Host "Upgrading agent on the lab system: $($vm.Name), machineName: $($vm.ComputerName)"
   $vm.InstallTestAgent($adminInfo, $adminInfo);

$op = $environment.Repair($environment.LabSystems)

$environment = $labService.GetLabEnvironment($environment.Uri)
Write-Host "Waiting for $($env.LabOperation.Name) to finish." -NoNewline

while($environment.LabOperation.OperationState -eq "InProgress")
  Start-Sleep -s 20
  Write-Host "." -NoNewline
  $environment = $labService.GetLabEnvironment($environment.Uri)

Write-Host "$($env.LabOperation.OperationState)" -NoNewline

Comments (4)
  1. STFAM says:

    Hi Aseem,

    I'm new to PowerShell and not very familiar with the TFS Client API calls.  I have a question about the AccountInformation method above.  If my test agent is in a Workgroup, what should I set the $domainName string to?  I have tried passing in an empty string, but for InstallTestAgent threw an exception indicating that I'm calling it with 2 arguments.  I'm not sure what it meant by that.



  2. STFAM says:


    I was able to resolve the exception error from InstallTestAgent above, but now the Repair method take a long time to complete then eventually fails with "TFS259641: To use the environment, you must install a compatible test agent in all machines of the environemt.  Click 'Install Agents' to complete this task." when viewing error details from MTM.  On the Test Controller, VSTTController is logging "Error during DsBind operation: 1355" in the Event Log.  My TFS server is in a domain where the Test Controller and all Test Agents are in a Workgroup.  I have an account set up on all machines with the same password so I use this account when creating the new lab environment.  Can anyone please tell me if there are specific requirements when creating a standard environment programmatically in a workgroup?

    Thank you.


  3. Subrahmanyam [MSFT] says:

    Hi Sam,

    Is your target VM is installed with Test Agent, can you check it from add remove programs. You can mail me @ for further discussion on this

  4. STFAM says:

    Problem was resolved by setting the environment CodedUI properties and setting the login credentials before creating the environment.

Comments are closed.

Skip to main content