This article describes the Azure automation processes such as Runbook that can help manage VMs automatically, and enabling you to schedule and perform VM start and stop operations without human intervention. Automating VM operations will help you save huge amount of time every day and so you don’t even have to worry about turning your VM on and off outside office hours. This will help you cut down at least 14 hours of usage per day, therefore you’re roughly saving almost 40-60% of your cloud infrastructure cost.

Microsoft Azure and Automation:

When you plan to schedule the start and shutdown of VMs, it allows you to save money on Azure costs which includes the price of computing.  A VM that has been shut down and deallocated does not incur any Azure charges (except for the space it occupies in blob storage).  Shutting down VMs is usually beneficial for different teams in your organizations like Dev, QA, Test, Staging and other non-production environments, CI/build servers, or for production environments where access to VMs is required only during specific times.

It’s again one of the cool features of MS Azure which brings high available infrastructure whenever you need and multiple tasks are carried out with no human intervention and even scheduled automatically at regular intervals.

Automate all your frequent activities like VM start and stop process, scale up/down and integrations which involve more time-consuming and error-prone cloud management tasks.

Azure Automation helps you spend more of your time focused on work that adds business value. By reducing errors and boosting efficiency, it can also help lower your operating costs.

Azure VM Automation

Using Windows PowerShell and graphical workflow allows more visibility to the business and infrastructure. In Azure Automation, Windows PowerShell workflows—or runbooks—helps to work smarter by handling the creation, deployment, monitoring, and maintenance of Azure resources and third-party applications.

The Automation Runbook Gallery puts samples, utilities, and scenario Runbook right at your fingertips, so you can get up and running quickly with your automation tasks. The Runbook Gallery lets you browse and import Runbook(s) to your Automation account without ever having to leave the Azure portal.

Each Runbook in Azure Automation has a Draft and a Published version. Only the Published version is available to be run, and only the Draft version can be edited. The Published version is unaffected by any changes to the Draft version. When the Draft version is ready to be available, then you publish it which overwrites the Published version with the Draft version.

Create an Automation Account in Azure portal:

****************************************

More services —> search automation —> select an Automation Account

blog-2

Once you have selected an Automation Account then create a new account for automation

blog-3

Once this is done, we have created an azure automation account

blog-4

Create a sample code to start and stop the VM using runbook through power-shell script.

Create a Runbook Account:

Select a Runbook in automation account, and then create a new Runbook account using a power-shell type.

blog-5

Once you create a run book account, open it and select the edit button that navigates to a new pane to paste your code (power-shell) script for starting the VM automatically

blog-6

blog-7

Click on Edit to paste your code and conditions to start the VM with azure automation credentials. The test pane used to test your code. Once you’ve got “Testing is succeeded” then it’s good to go ahead. The Script is perfect to save a script and publish a script to overwrite the existing code.

Schedule VM:

If you want to shut down and start Azure VMs really a true budget for Cloud infrastructure, so plan your business requirements very clearly.

When you’re releasing the VM i.e., shut down your VM then IP Address is deallocated.  When it is brought back up, it will get a new IP unless it is assigned an Azure Reserved IP. Please make sure that you’re not binding IP’s, instead of using FQDN (Fully Qualified Domain Name) in your cloud infrastructure.

For example, you need to start the VM only during business hours which is 8 Am to 6 PM Indian Standard Time after which you should have it turned off automatically. During weekends and off business hours, it will be stopped automatically as per the script and workflow.

Holiday Plan:

 Also, if you wish to plan for your holidays, vacations and business needs to schedule turn on/turn off your VMs, you can simply modify the rules and add it in power shell script. For example, if you wish to stop your VM’s on common holidays like Christmas and New Year, it can be possible.

blog-8

While some might argue that writing PowerShell is faster than using a graphical designer, I think there is still a good use for a graphical view of Runbook/workflows. Each of these stages could be connected together using the graphical designer, to keep a new overview of the entire process.

blog-9

blog-10

Sample Power-shell Script to Start VM

 

#workflow StartAzureVM {

#    inlineScript {

 

$connectionName = “AzureRunAsConnection”

try

{

# Get the connection “AzureRunAsConnection ”

$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName

 

“Logging in to Azure…”

Add-AzureRmAccount `

-ServicePrincipal `

-TenantId $servicePrincipalConnection.TenantId `

-ApplicationId $servicePrincipalConnection.ApplicationId `

-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint

}

catch {

if (!$servicePrincipalConnection)

{

$ErrorMessage = “Connection $connectionName not found.”

throw $ErrorMessage

} else{

Write-Error -Message $_.Exception

throw $_.Exception

}

}

 

$day = (Get-Date).DayOfWeek

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

exit

}

 

# Start the ASPIRE TEST VM

$vmName = “ASPIRE-DEMO-VM”

$rgName = “ASPIRE-DEMO-ENV”

$PowerState = (Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName -Status).Statuses.Code[1]

 

if ($PowerState -eq ‘PowerState/Running’)

{

Write-Output “‘$vmName’ is already started.”

}

else

{

Write-Output “‘$vmName’ is being Start up.”

$StartupState = (Start-AzureRmVM -ResourceGroupName $rgName -Name $vmName).IsSuccessStatusCode

if ($StartupState -eq ‘True’)

{

Write-Output “‘$vmName’ has been Started successfully.”

}

else

{

Write-Output “‘$vmName’ has failed to start. Start Status  = ” + $StartupState

}

}

 

# Start the ASPIRE TEST VM 2

 

$vName = ” ASPIRE-DEMO-VM 2″

$rName = ” ASPIRE-DEMO-ENV 2″

$PowerState = (Get-AzureRmVM -ResourceGroupName $rName -Name $vName -Status).Statuses.Code[1]

 

if ($PowerState -eq ‘PowerState/Running’)

{

Write-Output “‘$vName’ is already started.”

}

else

{

Write-Output “‘$vName’ is being Start up.”

$StartupState = (Start-AzureRmVM -ResourceGroupName $rName -Name $vName).IsSuccessStatusCode

if ($StartupState -eq ‘True’)

{

Write-Output “‘$vName’ has been Started successfully.”

}

else

{

Write-Output “‘$vName’ has failed to start. Start Status  = ” + $StartupState

}

}

Top 4 business benefits of using Azure Automation:

1.Easy to configure and save time

Repeated processes can be implemented as run books which can perform multiple tasks with no human intervention and even schedule them to be automatically performed at regular intervals by scripts.

2.Saving your cloud cost:

When you’re planning your Azure Automation it helps you spend more of your time focused on work that adds business value, when you’re clearly defining business requirements and schedules. By reducing errors and boosting efficiency, it can also help lower your operating costs (TCO).

3.Easy integration and Monitoring:

Once you configured the automation then notify the business users with whom you want to update. You can also use them with any service that offers public Internet APIs. Easy-to-read dashboard charts and log records make run books easier to monitor and keep all of them alert.

4.Faster delivery

Azure Automation lets you deliver services faster and more consistently. It’s highly reliable, and you can create checkpoints to resume your workflow after unexpected errors, crashes, and network issues