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("
http://myserver:8080/tfs/defaultcollection");
$projectName = "myproject";
$environmentName = "myEnvironment";
$testControllerName = "mycontrollerName:6901";

$machine1 = "mymachine.mydomain.com";
$machine1Role = "TestMachine";

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

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

# 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];
$machinesD.Add($machine1D);

# 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