How to list down all the “importable” virtual machines/templates programmatically?
MTM provides an ability to import one or more virtual machine/template to your team project. In that flow, it shows you complete list of virtual machines/templates that are available for you to choose from (see below image).
There could be scenarios where you would want to get this complete list programmatically. For those scenarios, please follow the following 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 emit out an output similar to the following one.
Enjoy !!
Output
TestVM2 VirtualMachine myHost.mydomain.com_Golden
W2012GOLD Template myHost.mydomain.com_Golden
Subbu_Win8 Template myHost.mydomain.com_Golden
W8Agent-02-04-2014 Template myHost.mydomain.com_Golden
Windows2k8R2-1 Template myHost.mydomain.com_Golden
Win2012RTM Template myHost.mydomain.com_Golden
Win8GOLDEN Template myHost.mydomain.com_Golden
AdminOpsVM1 VirtualMachine myHost.mydomain.com_PermissionModel
AdminOpsVM2 VirtualMachine myHost.mydomain.com_PermissionModel
Script
# Specify the tfs collection url and the projectName
$tfsCollectionUrl = New-Object System.URI(" https://myserver:8080/tfs/myCollection");
$projectName = "myProject";
# 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 the library shares
$libraryShareQuerySpec = New-Object Microsoft.TeamFoundation.Lab.Client.TeamProjectLibraryShareQuerySpec;
$libraryShareQuerySpec.Project = $projectName;
$libraryLocationList = $labService.QueryTeamProjectLibraryShares($libraryShareQuerySpec);
foreach ($libraryLocation in $libraryLocationList)
{
$machineQuerySpec = New-Object Microsoft.TeamFoundation.Lab.Client.VirtualMachineQuerySpec;
$machineQuerySpec.Location = $libraryLocation.Uri;
$virtualMachines = $labService.QueryVirtualMachines($machineQuerySpec);
foreach ($virtualMachine in $virtualMachines)
{
$type = "VirtualMachine"
$isTemplate = $virtualMachine.IsTemplate
if ($isTemplate -eq $TRUE) { $type = "Template" }
Write-Host $virtualMachine.VMName $type $libraryLocation.Name;
}
}