Using Azure Schedulers to automate stop and start of Azure Virtual machines

I just had some discussion around the practices being followed to save cost using Azure VMs. One of the discussion was around, development and test servers which are used just on weekdays and they are idle during weekends. The practice which administrators follow is, they automate the snooze the machine for Saturday/Sunday and start of the machine before the work hours on Monday.

In this post, I am going to share the script which you can use to create this task for Azure VMs :

1. Download makecert.exe using this link

image

2. Using mmc , export the certificate in both .cer and .pfx format

image

 

Export the certificate –> right click on the certificate –> all tasks –> export (use both the options one at time and create .cer and .pfx file):

image

 

3. Create automation account:

image
4. Click on the automation account –> go to assets  -> add setting -> create credential using .pfx file:

image

 

Add Certificate to management certificates section under settings:

 image

 

5. Create variables for CertificateName , SubscriptionId , ServiceName , VMName and subscriptionName – add the correct value for each variable (Click on the automation account –> go to assets  -> add setting –> Add variable –>String)

image sc

6. Create PowerShell runbooks using the scripts below:

Start_vm.ps1 (copy the below script into a notepad file and save it with ps1 extn)

workflow Start-VM

{

Param (

[parameter(Mandatory=$true)]

[String]

$VMName,

[parameter(Mandatory=$true)]

[String]

$ServiceName

)

$day = (Get-Date).DayOfWeek

if ($day -eq ‘Sunday’){

exit

}

$subscriptionName = Get-AutomationVariable -Name “SubscriptionName”

$subscriptionID = Get-AutomationVariable -Name “SubscriptionID”

$certificateName = Get-AutomationVariable -Name “CertificateName”

$certificate = Get-AutomationCertificate -Name $certificateName

Set-AzureSubscription -SubscriptionName $subscriptionName -SubscriptionId $subscriptionID -Certificate $certificate

Select-AzureSubscription $subscriptionName

Start-AzureVM -Name $VMName -ServiceName $ServiceName

}

Stop_vm.ps1 (copy the below script into a notepad file and save it with ps1 extn)

workflow Stop-VM

{

Param (

[parameter(Mandatory=$true)]

[String]

$VMName,

[parameter(Mandatory=$true)]

[String]

$ServiceName

)

$day = (Get-Date).DayOfWeek

if ($day -eq ‘Saturday’ -or $day -eq ‘Sunday’){

exit

}

$subscriptionName = Get-AutomationVariable -Name “SubscriptionName”

$subscriptionID = Get-AutomationVariable -Name “SubscriptionID”

$certificateName = Get-AutomationVariable -Name “CertificateName”

$certificate = Get-AutomationCertificate -Name $certificateName

Set-AzureSubscription -SubscriptionName $subscriptionName -SubscriptionId $subscriptionID -Certificate $certificate

Select-AzureSubscription $subscriptionName

Stop-AzureVM -Name $VMName -ServiceName $ServiceName -Force

}

 

7. Upload these workbooks under runbook section on the same page:

image

8. Screen will be like :

image

9. Publish the Runbooks – just click on the runbook name and then publish the script as follows:

image

just click on test before publishing it.

10. Once it’s published , click on schedule and then schedule it:

image

 

Guest Post:

Written by Gaurav Srivastava

Advertisements

Leave a Reply

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

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s