Automate test environment with Azure Virtual Machines

Setting up a test environment in Windows Azure can be automated with powershell. Below are some parts of my script.


You can download and install the Azure PowerShell modules by running the Microsoft Web Platform Installer.
The first step in my scripts is to connect to my subscription.

$subscription="Visual Studio Premium met MSDN"
Set-AzureSubscription -SubscriptionName $subscription

The first statement (Add-AzureAccount) will prompt you for the credentials, the second will select the subscription to use. I’m using a MSDN subscription with some free azure credit.

Virtual Network

Best practice is to create a virtual network. In my VM on Virtual Network post I’ve created a virtual network. You can export the settings to file. That file is imported in the script below.

Set-AzureVNetConfig -ConfigurationPath $networkConfig

The script can be run multiple times, when the network already exists a warning is displayed.

Virtual Machine

Now create the virtual machine. Select the Image to use (Windows Server 2012 R2 …) and the storage account and the credentials and the ports. A lot going on here, but that is why I use automation. Make sure to clean up previous created virtual machines (line 11)

$service = "mytestenvironment" #must be unique
$size = "Small"
$un = "boss" #some valid username
$pwd = "p@ssw0rd" #some valid password
$image = Get-AzureVMImage | where {$_.Label -eq 'Windows Server 2012 R2 Datacenter, October 2014'}
$storage = "myteststorage"
$adminvm = "admin"
# create the storage (errors are ignored)
New-AzureStorageAccount -StorageAccountName $storage -Location $location -ErrorAction Ignore
# remove old test virtual machine
Get-AzureVm -ServiceName $service -Name $adminvm -WarningAction Ignore | Remove-AzureVM -DeleteVHD
# set the storage account for creating the new vm
Set-AzureSubscription -SubscriptionName $subscription -CurrentStorageAccountName $storage
# create the new vm with public port 80, credentials and image on the virtual network
New-AzureVMConfig -Name $adminvm -InstanceSize $size -ImageName $image.ImageName |
  Add-AzureProvisioningConfig -Windows -AdminUsername $un -Password $pwd |
  Add-AzureEndpoint -Protocol tcp -LocalPort 80 -PublicPort 80 -Name 'Web' |
  New-AzureVm -ServiceName $service -VNetName $network -Location $location -WaitForBoot

Powershell Session

By default windows azure adds a powershell endpoint to new virtual machines. I use this endpoint to start a powershell session.

$secPassword = ConvertTo-SecureString $pwd -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($un, $secPassword)
#Less secure, but no certificate needed
$option = New-PSSessionOption -SkipCACheck
#Get the powershell endpoint/uri
$uriadmin = Get-AzureWinRMUri -ServiceName $service -Name $adminvm
#Start powershell session with credentials
$sessionAdmin = New-PSSession -ConnectionUri $uriadmin -Credential $credential -SessionOption $option

Configure and install software

Now I have a running Virtual Machine and a powershell session to it.
First I’ll punch some holes in the firewall for communication between the virtual machines over the virtual network.

Invoke-Command -Session $sessionAdmin -ScriptBlock {
    New-NetFirewallRule -Direction Inbound -Action Allow -LocalPort 31,32,33,80 -Protocol TCP -DisplayName "Some Service Ports"

Then install some windows features

Invoke-Command -Session $sessionAdmin -ScriptBlock {
    install-windowsfeature web-server
    install-windowsfeature web-metabase
    install-windowsfeature web-asp-net45

Finally install your software and start running your tests.


Before the October 2014 release of the Windows 2012 R2 image I needed to RDP into the machine or my software (MSI) installation would fail.


About erictummers

Working in a DevOps team is the best thing that happened to me. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Tooling and tagged , , , . Bookmark the permalink.

2 Responses to Automate test environment with Azure Virtual Machines

  1. Pingback: Setup logging in configuration with powershell | .NET Development by Eric

  2. Pingback: Powershell Functions in my Azure setup script | .NET Development by Eric

Leave a Reply

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

You are commenting using your 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.