This is a multi-part post for programmatically automating Microsoft Lab Center 2010 / Visual Studio Lab Management 2010 / Lab Manager 2010 (not VS/TFS2012) via C#:
The entry point to the API / SDK documentation is here via the GetService call (LabService is the main one, but also see LabAdminService and LabFrameworkService)
Getting in…
When an environment is running within Lab Center, how do you programmatically ‘get inside’ that Environment and find out the names of the running machines? What about the internal and external computer names? In the case of a Network Isolated environment that has been deployed, you need to know the world-facing DNS and IP Address name so that you can connect to it remotely or initiate custom deployments.
How to do this?
I’ve modified the UI so that each deployed environment lists it’s virtual machines as children. Each LabEnvironment object has a LabSystems property containing a list of all virtual machines contained within. I mean, really: could it be any easier?! Of interest to us are the ‘LabSystem.IsolationOptions’ through which we can find the Internal and External computer names:

The code of interest to us is:
// ls==LabSystem
if (ls.ExtendedInfo != null)
{
string roles = String.Format("Roles: {0}", ls.Roles);
string hostOs = String.Format("Guess Operation System: {0}", ls.ExtendedInfo.GuestOperatingSystem);
string hasVmAdditions = String.Format("Has Vm Additions: {0}", ls.ExtendedInfo.HasVMAdditions);
// This is the Hyper-V System hosting this virtual machine.
string hostname = String.Format("Virutalization Host Name: {0}", ls.ExtendedInfo.HostName);
string remoteSessionInfo = "Remote Session Info: (Not Available)";
if (ls.ExtendedInfo.RemoteInfo != null)
{
// The DNS name we will use to connect to a machine in a Network Isolated environment.
string computerName = String.Format("Computer Name: {0}", ls.ExtendedInfo.RemoteInfo.ComputerName);
// The internal name of the machine on the Isolated Network.
string internalName = String.Format("Internal Name: {0}", ls.ExtendedInfo.RemoteInfo.InternalComputerName);
remoteSessionInfo = String.Format("Remote Session Info:\r\n{0}\r\n{1}", computerName, internalName);
}
string vmInformation = String.Format("Extended Info:\r\n{0}\r\n{1}\r\n{2}\r\n{3}\r\n{4}\r\n", new object[] { hostOs, hasVmAdditions, hostname, remoteSessionInfo, roles });
From the LabSystem and LabSystem.ExtendedInfo objects you can get at the OS Profile, hardware profile, roles, machine ID’s so I’ve provided a subset of those properties at the bottom… in fact, you can get at *EVERYTHING* that you normally set up and enter when you create a new environment with Lab Center. It’s easy, and it’s very easy to relate those properties to what you see in the UI.
What good is this? A lot! After deploying or connecting to an environment you can ‘discover’ the environment, identify custom properties associated with it at the time of deployment and direct interaction with it. With this information, it is trivial to use RDP to connect through to the machine in question (I have added a ‘Connect’ button to the bottom of the dialog). In this example, I am launching MSTSC but you might be better off using an embedded RDP6 Interop so you can host the RDP session directly within your application (when using Lab Center and “Connect” to a machine the large view on the right hand side is just a hosted RDP Active-X Control…):
string RdpPath = Environment.ExpandEnvironmentVariables(@"%windir%\system32\mstsc.exe");
ProcessStartInfo psi = new ProcessStartInfo();
psi.Arguments = String.Format("/v:{0}", ls.ExtendedInfo.RemoteInfo.ComputerName);
psi.FileName = RdpPath;
Process.Start(psi);
But something is missing! When using Lab Center, you get a really cool Thumbnail view of each host:
I WANT THAT! Where does that come from?! Not from Lab Center: it comes from Hyper-V. There is a class called ‘SystemThumbnail’ in *.LEViewer.DLL but didn’t pursue it because it’s not part of the API Documentation I could find. Instead, I found a post that explains how you obtain an image of a running environment on Hyper-V. I won’t elaborate (all the code is there and *ALL* credit goes to that guy):
I modified a few things so that it did not actually save a Thumbnail file – instead, I just extract the returned Bitmap and assign it as the BackgroundImage on the control:
Thumbnail thumbnail = new Thumbnail(Width, this.Height); Bitmap result = thumbnail.GetVirtualSystemThumbnailImage(LabSystem.VMName, LabSystem.ExtendedInfo.HostName); this.BackgroundImage = result;
It is set to refresh every 5 seconds at the moment and you need to have a virtual machine (not environment) selected for it to appear… be patient
Recall that when you deploy an Environment in Lab Center, you chose a Host Group to apply it to – the ultimate Hyper-V Host it was deployed to (based on how SCVMM calculated the resources and suitability for the environment you are deploying) can be obtained with a call to theLabSystem.ExtendedInfo.HostName.
I’ve wrapped the whole thing up in ‘ThumbnailControl.cs’ (without any error checking) which works the same as ‘EnvironmentMonitorControl.cs’ – you Bind a Virtual Machine to it, and every few seconds it will refresh the display.
Tchau!















