Share via


Provisioning and de-provisioning machines in Azure Virtual Machines

Now that we have a couple of Virtual Machines running in the cloud, the costs are continuously running. The good thing with the cloud is that we only pay for the machines that we need.
But wouldn't it be nice to pay only when you need it too?

Just one important piece of information. Stopping the machine will not stop the meter, so you will have to delete the virtual machine.

But you don't have to delete the disk, so you will still keep all your stuff. And you will be up and running in no time when you need to. Sweet :)

One of my colleagues created some simple powershell scripts to do this, and it works perfectly.

The first part of it is a config file that holds the parameters of the Azure service and the local path to store the VM configuration. And also information about virtual network and affinity group.

The settings

The file is named Settings.ps1 and here is the content.

$VMPath = '<local path to store the VM configuration>'
$VirtualNetwork = "<Azure Virtual Network>"
$AffinityGroup = "<Affinity group>"
$AzureServiceName = "<Azure Service>"

Exporting and removing the VM configuration

The second part is a script for exporting the configuration and remove the VMs related to the service in $AzureServiceName. I called the file RemoveVMs.ps1

. .\Settings.ps1

write-host "Exporting settings and removing all VMs for Azure Service $AzureServiceName" -ForegroundColor Yellow
write-host "Export path = $VMPath" -ForegroundColor Yellow
Get-AzureVM | where {$_.ServiceName -eq $AzureServiceName} | foreach { 
     $path = $VMPath + '\' + $_.Name + '.xml'
     Export-AzureVM -ServiceName $_.ServiceName -Name $_.Name -Path $path
     Remove-AzureVM -ServiceName $_.ServiceName -Name $_.Name
}
write-host "Removing Azure Service $AzureServiceName" -ForegroundColor Yellow
Remove-AzureService -ServiceName $AzureServiceName -Force

 

As you can see, the script imports the settings. Then it gets all VMs in the service, export the settings. Then it removes the VMs and in the end the service itself.

It is a good idea to test the script with the two remove commands commented out first to ensure that the configuration is exported correctly. You can then run it again with the remove enabled.

 

Importing the VM configuration

The last part is the script to import the configuration and start up the machines again. I called the file ImportVMs.ps1

. .\Settings.ps1

write-host "Restoring VMs in Azure Service $AzureServiceName" -ForegroundColor Yellow
write-host "Import path = $VMPath" -ForegroundColor Yellow

Get-ChildItem $VMPath\*.xml | foreach {
    $path = $_
    Write-Host $path
    $vm = Import-AzureVM -Path $path
    $azureService = Get-AzureService | WHERE {$_.ServiceName -eq $AzureServiceName }
    if ($azureService -eq $null)
    {
        $vm | New-AzureVM -ServiceName $AzureServiceName -AffinityGroup $AffinityGroup -VNetName $VirtualNetwork
    }
    else
    {
        $vm | New-AzureVM -ServiceName $AzureServiceName -VNetName $VirtualNetwork       
    }
}

This script takes the config files that you have exported and creates the VMs in Azure again. The VMs will start up when finished.

And that's it :)

 

Gathering machines under one subscription

So I promised in the last article to pick up on how to gather more machines under one subscription.

Here you can experiment on creating new scripts from the ones already provided to streamline the prosess. But what I find easiest is to just temporarily change the settings file to export the new machine.

Let's say I have created a new VM from the Azure Web UI and the machine is named palbenNEW. In the Settings.ps1 file you change $AzureServiceName to "palbenNEW". The palbenNEW service is hidden, so you will not see it in the interface. Unless you delete the VM in the, that will not delete the attached service, but make it visible.

Now you can run the RemoveVMs script (try exporting without the removes first) and it will export your configuration, putting it in the folder with the other machines.

Now change the $AzureServiceName back again.

When you now run the import script, it will put the last machine in the same service as the other ones.

More information

If you look at this session from the recent online Windows Azure Conference, you can pick up loads of other nice tips and tricks.
https://channel9.msdn.com/Events/windowsazure/Windows-AzureConf-2013/Essential-Iaas-for-Developers