In our previous article, I explained how to write a simple PowerShell script to expand the size of an Azure VM.
- PowerShell script to expand and reduce the size of an Azure VM
- Change the size of Azure VM in a given schedule using Azure Runbook
In this article I’ll explain how to execute PowerShell scripts in a given schedule using Azure Automation Account, Runbooks and Schedules.
What is an Azure Automation?
Azure Automation is a Software as a Service (SaaS) that can be used to automate processes. You can automate cloud automation tasks using Azure Automation. Some common tasks are,
- Build and deploy resources
- Configure VMs
- Monitor resources
What is a Runbook?
Azure Automation use Runbooks to automate processes. Runbooks are executed in automation Sandboxes, which means as a Platform as a Service (PaaS) VMs. You will get the isolation that you expect from Runbooks
Following are the steps I used to expand and reduce the size of VMs in my Azure tenant.
1. Create Azure Automation Account
![image image](https://lh3.googleusercontent.com/-pDPAJyPDmsU/Wog7eY2AKII/AAAAAAAAC8M/fwmPRY1sDTwN_HSCR10tM3xgLMrzSPrdgCHMYCw/image_thumb%255B2%255D?imgmax=800)
Navigate to Variables section and create following variables
![image image](https://lh3.googleusercontent.com/-1Yy9L0WJxU0/Wog7gi76x5I/AAAAAAAAC8U/OQIH43Q8UnQ_9gNTpJhY1XHi6Jcl-Y9DACHMYCw/image_thumb%255B6%255D?imgmax=800)
![image image](https://lh3.googleusercontent.com/-0TgBY8CnvR0/Wog7jls23jI/AAAAAAAAC8c/VDZyo5puhBMoKed7xRTckvO-2-Vh353TgCHMYCw/image_thumb%255B9%255D?imgmax=800)
2. Create the Runbook
![image image](https://lh3.googleusercontent.com/-SpFvtyZTBXU/Wog7l7zLjzI/AAAAAAAAC8k/Ax7gCtpaDHg7NkGtHbVplmUikwd5_uchwCHMYCw/image_thumb%255B12%255D?imgmax=800)
Create new Runbook as a PowerShell script to expand the size
![image image](https://lh3.googleusercontent.com/-0_NCofgEEys/Wog7ojeZpUI/AAAAAAAAC8s/6B1PuTxaI4wS6AohsEq864uAxO8hSsjeQCHMYCw/image_thumb%255B15%255D?imgmax=800)
Add following script
$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
}
}
$vmName = Get-AutomationVariable -Name 'vmName'
$resourceGroup = Get-AutomationVariable -Name 'resourceGroup'
$lowHwProfile = Get-AutomationVariable -Name 'lowHwProfile'
"Updating VM size"
Get-AzureRmVMSize -ResourceGroupName $resourceGroup -VMName $vmName
$vm = Get-AzureRmVM -ResourceGroupName $resourceGroup -Name $vmName
$vm.HardwareProfile.VmSize = $lowHwProfile
Update-AzureRmVM -VM $vm -ResourceGroupName $resourceGroup
"VM size updated"
Same way create another Runbook to reduce the VM size
3. Create Schedules and apply to Runbooks
Click on Schedules in Automation Account
![image image](https://lh3.googleusercontent.com/-Dfw0Ua03EUk/Wog7rI1MoII/AAAAAAAAC80/pVbs0_OE_g8a1pxaE5K_osx024BLkQLGQCHMYCw/image_thumb%255B17%255D?imgmax=800)
![image image](https://lh3.googleusercontent.com/-MTsbgF6qpUY/Wog7tWt1etI/AAAAAAAAC88/Xlxr5ajILLcdJyk3NIACTHtFjYJRdAfSwCHMYCw/image_thumb%255B20%255D?imgmax=800)
Navigate to specific Runbook to apply the schedule
![image image](https://lh3.googleusercontent.com/-EV3LGtrjFZo/Wog7wG62-YI/AAAAAAAAC9E/8FpTppYkaNsaw4Z5ZmU5UHJohy6p9rN0gCHMYCw/image_thumb%255B25%255D?imgmax=800)
![image image](https://lh3.googleusercontent.com/-6emOMZkB7MM/Wog7yi7kYUI/AAAAAAAAC9M/GIi_qVxwiX8uK2JdNJQupcNzyO1x5b3fQCHMYCw/image_thumb%255B26%255D?imgmax=800)
That’s it. We have to create another schedule to reduce the size of the VM on Monday night and apply to the particular Runbook