Freigeben über


How to mark an environment in use?

MTM provides a nice Ux to mark the environments in use but one of our customer wanted to do that programmatically. Here is the script for it with execution steps: -

  • 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 mark the environment in use if it is not already marked.

Enjoy !!

# Define parameters
$tfsCollectionUrl = New-Object System.URI("
https://myserver:8080/tfs/defaultcollection");
$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;
$labEnvironmentQuerySpec.Disposition = [Microsoft.TeamFoundation.Lab.Client.LabEnvironmentDisposition]::Active;

$labEnvironments = $labService.QueryLabEnvironments($labEnvironmentQuerySpec);
foreach ($environment in $labEnvironments)
{
$envName = $environment.Name;
if ($envName -eq $environmentName)
{
$matchingEnvironment = $environment;
}
}

# whether the environment is already in use
$inUseMarker = $matchingEnvironment.GetInUseMarker();

if ($inUseMarker -eq $null)
{
Write-Host Environment is not in-use;

   $matchingEnvironment.SetInUseMarker("Reserving the environment for script testing purpose");
}
else
{
Write-Host Environment is in-use since $inUseMarker.Timestamp by $inUseMarker.User with details $inUseMarker.Comment;
}