Freigeben über


How to cancel an ongoing operation on lab environment programmatically?

MTM provides users to do bunch of operations on lab environments and a lot of these operations are long running. MTM also provides a gesture to cancel the operation but in case you want to cancel it programmatically, you can use the following script. 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 cancel the operation

Enjoy !!

# Define parameters
$tfsCollectionUrl = New-Object System.URI("
https://myserver:8080/tfs/mycollection");
$projectName = "myproject";
$environmentName = "myEnvironment";

# 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 for environments
$labEnvironmentQuerySpec = New-Object Microsoft.TeamFoundation.Lab.Client.LabEnvironmentQuerySpec;
$labEnvironmentQuerySpec.Project = $projectName;

$labEnvironments = $labService.QueryLabEnvironments($labEnvironmentQuerySpec);
foreach ($environment in $labEnvironments)
{
$envName = $environment.Name;
if ($envName -eq $environmentName)
{
$environment.CancelCurrentOperation();
Write-Host Operation cancelled
}
}