Using Friendly DNS Names in Visual Studio 2010 Lab Management


If you’ve spent some time with Lab Management, specifically where you’ve leveraged Network Isolation you will have noticed the nice fully qualified domain names assigned to the external network adapter identified in the diagram below as “Computer name:”.

System info

System info

If you want to create easier to remember names for your VMs you could create some CNAMEs in your DNS so you can have a name like iis-01.dev.lab.tfs.domain.local instead of VSLM-58-62ff5f0e-3584-4fed-b726-5741bb62e659.domain.local of course this is quite a bit of effort if you have many environments and machines or recreate your environment.

Lab Center Environment

Lab Center Environment

In my DNS Manager I have created a few subdomains as containers for virtual machine friendly names in Lab Management Environments.

DNS Manager | Subdomains

DNS Manager | Subdomains

Now that I have those subdomains I just need a really easy method to create CNAMEs based on the computers in my environments so I can get entries like these created for me.

Alias (CNAME)

Alias (CNAME)

I created a Powershell Script to iterate through a Lab Environment for a Team Project. In Lab Center I have enabled the display of the ID column for the Environments which I will use later in the Lab URI.

Environments with ID

Environments with ID

Here is a sample command-line:

.\CreateCnamesForLabEnvironment.ps1 -dnsServer ws08srv -flzone “domain.local” -aSuffix “lab.tfs.domain.local” -labUri “vstfs:///LabManagement/LabEnvironment/56” -tpcUrl “http://localhost/tfs/DefaultCollection”

Here is the Powershell script, use at your own risk.  Let me know if you find it useful. 
The script ignores the machine configured as the Domain Controller in the environment as it has no external adapter defined. The script gets the internal computer name to use as the friendly name.


# CreateCnamesForLabEnvironment.ps1
# helper function
function isNullOrEmpty {
param (
[string] $str
)
if ([system.string]::IsNullOrEmpty($str))
{
return $true
}
else
{
return $false
}
}
# http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/13/manage-dns-in-a-windows-environment-by-using-powershell.aspx
function newcname {
param(
[string]$dnsServer,
[string]$flzone,
[string]$labComputer,
[string]$alias
)
## split the hostname from the FQDN
$server = $dnsServer -split "\."
$rr = [WmiClass]"\\$($server[0])\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord"
##
## create CNAME
$text = "$alias IN CNAME $labComputer"
$rr.CreateInstanceFromTextRepresentation($dnsServer, $flzone, $text)
}

function Usage
{
Write-Host "Usage :"
Write-Host ""
Write-Host " CreateCnamesForLabEnvironment.ps1 [-dns ] [-flzone ] [-aSuffix ] [-labUri ]"
Write-Host ""
Write-Host "-dnsServer : DNS Server : dc01.domain.local"
Write-Host "-flzone : Forward Lookup Zone : domain.local"
Write-Host "-aSuffix : alias suffix : dev01.tfs.domain.local"
Write-Host "-labUri : URI for Lab Environment : vstfs:///LabManagement/LabEnvironment/470"
Write-Host "-tpcUrl : URL for Team Project Collection : http://localhost/tfs/DefaultCollection"

exit;
}

#Process command line parameters
for($i = 0; $i -lt $args.Length; $i++)
{
if($args[$i] -ieq "-dnsServer")
{
$i++;
$dnsServer = $args[$i]
}
elseif($args[$i] -ieq "-flzone")
{
$i++;
$flzone = $args[$i]
}
elseif($args[$i] -ieq "-aSuffix")
{
$i++;
$aSuffix = $args[$i]
}
elseif($args[$i] -ieq "-labUri")
{
$i++;
$labUri = $args[$i]
}
elseif($args[$i] -ieq "-tpcUrl")
{
$i++;
$tpcUrl = $args[$i]
}

else
{
Usage;
exit
}
}

## check DNS server contactable
if ($dnsServer -eq $null)
{
Throw "DNS null"
}
if (-not (Test-Connection -ComputerName $dnsServer))
{
Throw "DNS server not found"
exit
}

# Load Assemblies
[Reflection.Assembly]::Load("Microsoft.TeamFoundation.Client, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") | Out-Null
[Reflection.Assembly]::Load("Microsoft.TeamFoundation.Lab.Client, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") | Out-Null

# TFS 2010 and Lab Management Connections
$tpc = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tpcUrl);
$labService = $tpc.GetService([Microsoft.TeamFoundation.Lab.Client.LabService]);
$labEnv = $labService.GetLabEnvironment($labUri);

if ($labEnv.StatusInfo.State -eq "Running")
{
foreach($labSystem in $labEnv.LabSystems)
{
$computerName = $labSystem.ExtendedInfo.RemoteInfo.ComputerName
$internalComputerName = $labSystem.ExtendedInfo.RemoteInfo.InternalComputerName
$svr = $internalComputerName -split "\."
$alias = ($svr[0]+"."+$aSuffix)

if (-not(isNullOrEmpty($computerName)))
{
if (-not(isNullOrEmpty($internalComputerName)))
{
Write-Host "Creating a CNAME for "$computerName
Write-Host "Alias (CNAME):"$alias
newcname $dnsServer $flzone $computerName $alias
}
}
}
}
else {
Write-Host "The Lab Environment specified is not running!"
Write-Host "Lab URI: "$labUri
Write-Host "Environment Name: "$labEnv.Name
Write-Host "Environment State: "$labEnv.StatusInfo.State
}

About Wes MacDonald

Wes MacDonald is a DevOps Consultant for LIKE 10 INC., a DevOps consulting firm providing premium support, guidance and services for Azure, Microsoft 365 and Azure DevOps.

Trackbacks/Pingbacks

  1. ALM Links–07/18/2012 » ALM Rocks! - July 18, 2012

    […] Using Friendly DNS Names in Visual Studio 2010 Lab Management […]

    Like

  2. Technology Post Roundup – 2nd Edition « Jonathan Rozenblit - August 3, 2012

    […] Using Friendly DNS Names in Visual Studio 2010 Lab Management by Wes MacDonald Create Computer Names in your DNS that are shorter and easier to remember using this simple trick. […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: