Udostępnij za pośrednictwem


How to add/query tags on lab environment machines?

MTM provides the ability to add/view tags on lab environment machines which helps in associating interesting information with the machine. You can add that using the following Ux in MTM.

image

One of the internal customer asked me that he wants to query these tags programmatically. Here are the steps that you should follow to query it programmatically.

  • 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 show whether your environment is healthy or not. 

Enjoy !!

# Define parameters
$tfsCollectionUrl = New-Object System.URI("
https://myserver/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)
{
foreach ($labSystem in $environment.LabSystems)
{
foreach ($property in $labSystem.CustomProperties)
{
Write-Host $property.Key -> $property.Value;
}

         # You can query OS info as well.
#
$extendedInfo = $labSystem.ExtendedInfo;
$osProfile = $extendedInfo.OSProfile;
Write-Host $labSystem.DisplayName :: GuestOS -> $extendedInfo.GuestOperatingSystem;
}
}
}