MTM provides ability to see all the environments across your lab and one of the thing it shows is whether the environment is healthy or not. By healthy, I mean whether machines in the environment are ready to run tests or are ready to deploy an application or not. Yesterday one of our customer mentioned that she wants to find this out programmatically. So in this blog, I am sharing how to do that 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 !!

(Update: 18/6/2014 Jason Van Eaton, an internal customer pointed out a bug in the post where he correctly mentioned that I am checking for NotReady state twice and am not checking for Offline state. Fixed it now.)

# Define parameters
$tfsCollectionUrl = New-Object System.URI("
http://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)
      {
         if ($labSystem.State -eq [Microsoft.TeamFoundation.Lab.Client.TestMachineState]::NotReady -Or $labSystem.State -eq [Microsoft.TeamFoundation.Lab.Client.TestMachineState]::Offline)
         {
            Write-Host $envName -> $labSystem.DisplayName -> $labSystem.State -> UnHealthy;
         }
         else
         {
            Write-Host $envName -> $labSystem.DisplayName -> $labSystem.State -> Healthy;
         }

      }
   }
}