BLOG
Wissenstransfer von IT-Spezialisten
| |

Microsoft Azure: AzureVM wakeup/shutdown script

As many others I use Microsoft Azure for more and more reasons. Most of my test and demo environments appear in the Microsoft cloud and will be used anytime from anywhere.

The big advantage is the pay-as-you-go strategy. I just need to pay for the resources I really use. My MSDN account gives me the abillity to spend 75$ every month in azure for free 🙂

But on the downside, 75$ are used up very fast and so I just wanted to run my mashines if I really need them.

If you shutdown the VM on the Azure Portal it will be shut down and be deallocated. This means the compute resources are not reserved anymore. The VM will just consume storage resources and therefor be much cheaper.

As a result I wrote a small and simple powershell script to shut down and wakeup my VMs. I do not need my environment over night and prefer saving the money.

As a requirement the Azure Powershell Module has to be installed. Hit the link below:

http://azure.microsoft.com/en-us/documentation/articles/install-configure-powershell/

 

And here is the script that I actually use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Import-Module azure #azure module has to be installed
Add-AzureAccount #prompt for your azure credentials
#
#next we choose our operation
#this could be improved with "stop VM but stay provisioned" or "stopp all/ start all" operation
#
$Ops = @("Start VM(s)";"Stop VM(s)")
#with -passthrough parameter we could select a row (or rows, just hold crtl) and pass that object to the pipeline
$Op = $Ops | out-gridview -Title "Select Operation" -PassThru
#simple if/else "what we choose" statement
if ($Op -eq "Start VM(s)") {
#we query all vms with specific status and pass them to gridview to make them selectable and pass selected object to our start/stop operation
$VMs = Get-AzureVM | where {$_.Status -ne "ReadyRole"} | select Name, ServiceName, Status
$VM = $VMs | Out-GridView -Title "Azure VMs" -PassThru
$VM | Start-AzureVM
write-output "Starting VM(s)"
}
elseif ($op -eq "Stop VM(s)") {
$VMs = Get-AzureVM | where {$_.Status -ne "StoppedDeallocated"} | select Name, ServiceName, Status
$VM = $VMs | Out-GridView -Title "Azure VMs" -PassThru
$VM | Stop-AzureVM
write-output "Stopping VM(s)"
}
else {
Write-Output "something went wrong"
}
Get-AzureVM | select Name, ServiceName, Status |  Out-GridView -Title "Azure VMs" -Wait

 

I created a shortcut on my desktop with following commandline:

„powershell.exe -executionpolicy bypass -file „[scriptpath]“

I can execute it if I start or finnish my working day.

 

There are many possible improvements for the script. Ist just a quick version for doing my manual business. However it could have more operations like „shutdown all vms“ or a query for subscriptions (i just use one single subscription). Same goes for the error handling, which does not exist yet 😉

 

In conclusion, for me the script is doing well and helps a lot to save costs and time.