Setting up a test environment in Windows Azure can be automated with powershell. Below are some parts of my script.
Setup
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.
Add-AzureAccount $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.
$networkConfig="MyNewNetwork.xml" 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.
Quirks
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.
References
- How to install and configure Azure PowerShell on Microsoft Azure documentation
- My Quite install MSI with help from Orca post. Used to install my software.
- PowerShell Code Repository Send-File.ps1 to send files in a Session.
- Opening RDP session to an Azure VM with PowerShell
Pingback: Setup logging in configuration with powershell | .NET Development by Eric
Pingback: Powershell Functions in my Azure setup script | .NET Development by Eric