Blog

Create a Visual Studio Virtual Machine on Azure programmatically

12 Apr, 2016
Xebia Background Header Wave

There are multiple possibilities to create a development environment with Visual Studio on Azure. It can be created manually in the Azure Portal or programmatic with PowerShell.

Manually in the Azure Portal

You can create a development environment in the Azure Portal within minutes . Select the image with Visual Studio in the Azure Portal that best suites your requirements. At the time of writing, the latest version in the Azure Portal that can be selected is Visual Studio 2015 with update 2 and Azure SDK 2.9. It’s easy to select an image in the Portal, now let’s see how this can be done in code.

Virtual Machines - Microsoft Azure

Programmatic with PowerShell

When you want to create a Virtual Machine programmatically, PowerShell is your friend together with an ARM template.

This ARM template for example, will help you creating an image with Visual Studio. The ARM template contains a list of SKU’s to choose from, but unfortunately the Visual Studio 2015 with update 2 and Azure SDK 2.9. version is not present in the list of SKU’s. How should you know what SKU you’ve got to pass as a parameter? The name of the SKU is not visible in the Azure Portal.

This article tells you the steps to get a SKU. When you use PowerShell, the Cmdlets Get-AzureVMImagePublisher, Get-AzureVMImageOffer and Get-AzureVMImageSku mentioned in the article are not available. You should use the RM versions of the Cmdlets.

To get the complete list of all images that offers Visual Studio execute the following line:

Get-AzureRmVMImageSku -Location West Europe -Publisher “MicrosoftVisualStudio” -Offer “VisualStudio” | Select Skus

The result is the following list of SKU’s that offer Visual Studio.

Skus
 ----
 2013-Community-Update-4-ws2012-az25-ntvs10
 2013-Community-Update-4-ws2012-az26
 2013-Community-Update-4-ws2012-az26-cor31
 2013-Premium-Update-4-win81
 2013-Premium-Update-4-win81n-az26
 2013-Premium-Update-4-ws2012-az26
 2013-Professional-Update-4-ws2012-az26
 2013-Ultimate-Update-4-win81
 2013-Ultimate-Update-4-win81n-az26
 2013-Ultimate-Update-4-ws2012-az26
 2015-Community-RC
 2015-Enterprise-RC
 2015-Enterprise-Win10Tools
 2015-Professional-RC
 CoreCLR
 VS-2013-Comm-VSU5-AzureSDK-2.8-Win8.1-N-x64
 VS-2013-Comm-VSU5-AzureSDK-2.8-WS2012R2
 VS-2013-Comm-VSU5-Cordova-CTP3.2-AzureSDK-2.8-WS2012R2
 VS-2013-Community-VSU5-AzureSDK-2.7-Win8.1-N-x64
 VS-2013-Community-VSU5-AzureSDK-2.7-WS2012R2
 VS-2013-Community-VSU5-Cordova-CTP3.2-AzureSDK-2.7-WS2012R2
 VS-2013-Prem-VSU5-AzureSDK-2.8-Win8.1-N-x64
 VS-2013-Prem-VSU5-AzureSDK-2.8-WS2012R2
 VS-2013-Premium-VSU5-AzureSDK-2.7-SQL-WS2012R2
 VS-2013-Premium-VSU5-AzureSDK-2.7-Win8.1-N-x64
 VS-2013-Premium-VSU5-AzureSDK-2.7-WS2012R2
 VS-2013-Ultimate-VSU5-AzureSDK-2.7-SQL-WS2012R2
 VS-2013-Ultimate-VSU5-AzureSDK-2.7-Win8.1-N-x64
 VS-2013-Ultimate-VSU5-AzureSDK-2.7-WS2012R2
 VS-2013-Ultimate-VSU5-AzureSDK-2.8-Win8.1-N-x64
 VS-2013-Ultimate-VSU5-AzureSDK-2.8-WS2012R2
 VS-2015-Com-VSU1-AzureSDK-2.8-WS2012R2
 VS-2015-Comm-AzureSDK-2.8-Cordova-Win8.1-N-x64
 VS-2015-Comm-AzureSDK-2.8-WS2012R2
 VS-2015-Comm-AzureSDK-2.8-WS2012R2.1
 VS-2015-Comm-AzureSDK-2.8-WS2012R2.2
 VS-2015-Comm-VSU1-AzureSDK-2.8-W10T-1511-N-x64
 VS-2015-Comm-VSU1-AzureSDK-2.8-W10T-N-x64
 VS-2015-Comm-VSU1-AzureSDK-2.8-WS2012R2
 VS-2015-Comm-VSU1-AzureSDK-2.8-WS2012R2.1
 VS-2015-Comm-VSU2-AzureSDK-29-W10T-N-x64
 VS-2015-Comm-VSU2-AzureSDK-29-WS2012R2
 VS-2015-Community-AzureSDK-2.7-Cordova-Win8.1-N-x64
 VS-2015-Community-AzureSDK-2.7-W10T-Win10-N
 VS-2015-Community-AzureSDK-2.7-WS2012R2
 VS-2015-Ent-AzureSDK-2.8-Cordova-Win8.1-N-x64
 VS-2015-Ent-AzureSDK-2.8-WS2012R2
 VS-2015-Ent-AzureSDK-2.8-WS2012R2.2
 VS-2015-Ent-VSU1-AzureSDK-2.8-W10T-1511-N-x64
 VS-2015-Ent-VSU1-AzureSDK-2.8-W10T-N-x64
 VS-2015-Ent-VSU1-AzureSDK-2.8-WS2012R2
 VS-2015-Ent-VSU1-AzureSDK-2.8-WS2012R2.1
 VS-2015-Ent-VSU1-AzureSDK-2.8-WS2012R2.2
 VS-2015-Ent-VSU2-AzureSDK-29-W10T-N-x64
 VS-2015-Ent-VSU2-AzureSDK-29-WS2012R2
 VS-2015-Enterprise-AzureSDK-2.7-Cordova-Win8.1-N-x64
 VS-2015-Enterprise-AzureSDK-2.7-W10T-Win10-N
 VS-2015-Enterprise-AzureSDK-2.7-WS2012R2
 VS-2015-Pro-AzureSDK-2.8-Cordova-Win8.1-N-x64
 VS-2015-Pro-VSU1-AzureSDK-2.8-W10T-1511-N-x64
 VS-2015-Pro-VSU1-AzureSDK-2.8-W10T-N-x64
 VS-2015-Pro-VSU1-AzureSDK-2.8-WS2012R2.2
 VS-2015-Professional-AzureSDK-2.7-Cordova-Win8.1-N-x64
 VS-2015-Professional-AzureSDK-2.7-W10T-Win10-N
 VS-Next-Preview-Ent-AzureSDK-2.9-W10-N
 VS-Next-Preview-Ent-AzureSDK-2.9-WS2012R2

 

In my case, I’m able to choose between Visual Studio Enterprise or Community, and choose between Windows 10 or Windows 2012 R2. I selected the following SKU:

VS-2015-Ent-VSU2-AzureSDK-29-W10T-N-x64.

When you pass this SKU to the ARM template mentioned above, with the following script:

# Login to Azure
Login-AzureRmAccount
# Optionally select the correct subscription
Select-AzureRmSubscription -SubscriptionId "<YourSubscriptionId>"
# parameters to create a Visual Studio Image
$ResourceGroupName = "myresourcegroup"
$Location = "West Europe"
$VmName = "myVmName"
$vmAdminUserName = "myAdminUser"
$vmAdminPassword = "P@ssw0rd1"
$vmSize = "Standard_DS2"
$vmVisualStudioVersion = "VS-2015-Ent-VSU2-AzureSDK-29-W10T-N-x64"
$VmIPPublicDnsName = "myPublicDnsName"
# Create the resourcegroup
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location -Verbose -Force -ErrorAction Stop
# Execute the ARM template
New-AzureRmResourceGroupDeployment -Name ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm') `
 -ResourceGroupName $ResourceGroupName `
 -TemplateUri https://raw.githubusercontent.com/azure/azure-quickstart-templates/master/visual-studio-dev-vm/azuredeploy.json `
 -TemplateParameterObject @{deployLocation=$Location;vmName=$vmName;vmAdminUserName=$vmAdminUserName;vmAdminPassword=$vmAdminPassword;vmSize=$vmSize;vmVisualStudioVersion=$vmVisualStudioVersion;vmIPPublicDnsName=$vmIPPublicDnsName} -Force -Verbose

You will get the following validation error:

New-AzureRmResourceGroupDeployment : InvalidTemplate: Deployment template validation failed: ‘The provided value ‘VS-2015-Ent-VSU2-AzureSDK-2
9-W10T-N-x64′ for the template parameter ‘vmVisualStudioVersion’ at line ’60’ and column ’31’ is not valid. The parameter value is not part o
f the allowed value(s): ‘VS-2015-Pro-VSU1-AzureSDK-2.8-W10T-1511-N-x64,VS-2015-Pro-AzureSDK-2.8-Cordova-Win8.1-N-x64,VS-2015-Ent-VSU1-AzureSD
K-2.8-WS2012R2,VS-2015-Ent-VSU1-AzureSDK-2.8-W10T-1511-N-x64,VS-2015-Comm-VSU1-AzureSDK-2.8-WS2012R2,VS-2015-Comm-VSU1-AzureSDK-2.8-W10T-N-x6
4,VS-2015-Comm-AzureSDK-2.8-Cordova-Win8.1-N-x64,VS-2013-Ultimate-VSU5-AzureSDK-2.8-WS2012R2,VS-2013-Prem-VSU5-AzureSDK-2.8-WS2012R2,VS-2013-
Comm-VSU5-Cordova-CTP3.2-AzureSDK-2.8-WS2012R2,VS-2013-Comm-VSU5-AzureSDK-2.8-WS2012R2′.’.
At line:16 char:1
+ New-AzureRmResourceGroupDeployment -Name ((Get-Date).ToUniversalTime( …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmResourceGroupDeployment], CloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Resources.NewAzureResourceGroupDeploymentCommand

 

This is because there is a limited set of SKU’s available in the ARM template. To get around this you have to change the ARM template. First download the ARM template and save it with the name AzureDeploy.json. Add the missing SKU to the list of available SKU’s in the json (or delete them all, so there won’t be any validation on it).

Change the PowerShell script to execute the ARM template from locally:

# Login to Azure
Login-AzureRmAccount
# Optionally select the correct subscription
Select-AzureRmSubscription -SubscriptionId "<YourSubscriptionId>"
# parameters to create a Visual Studio Image
$ResourceGroupName = "myresourcegroup"
$Location = "West Europe"
$VmName = "myVmName"
$vmAdminUserName = "myAdminUser"
$vmAdminPassword = "P@ssw0rd1"
$vmSize = "Standard_DS2"
$vmVisualStudioVersion = "VS-2015-Ent-VSU2-AzureSDK-29-W10T-N-x64"
$VmIPPublicDnsName = "myPublicDnsName"
# Create the resourcegroup
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $Location -Verbose -Force -ErrorAction Stop
$TemplateFile = 'AzureDeploy.json'
$TemplateFile = [System.IO.Path]::Combine($PSScriptRoot, $TemplateFile)
# Execute the ARM template
New-AzureRmResourceGroupDeployment -Name ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm') `
-ResourceGroupName $ResourceGroupName `
-TemplateFile $TemplateFile `
-TemplateParameterObject @{deployLocation=$Location;vmName=$vmName;vmAdminUserName=$vmAdminUserName;vmAdminPassword=$vmAdminPassword;vmSize=$vmSize;vmVisualStudioVersion=$vmVisualStudioVersion;vmIPPublicDnsName=$vmIPPublicDnsName} -Force -Verbose

When you execute this PowerShell Script with the slightly changed ARM template, the image will be created on Azure.

Questions?

Get in touch with us to learn more about the subject and related solutions

Explore related posts