Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What steps are involved in creating a Runbook for VM shutdown


What steps are involved in creating a Runbook for VM shutdown


To create a Runbook for shutting down a virtual machine (VM) in Azure, follow these steps:

1. Create an Automation Account: First, ensure you have an Automation Account set up in Azure. If not, create one by navigating to the Azure portal, searching for "Automation Accounts," and clicking on "Add" to create a new account. Specify a name, resource group, and location for the account[3][4].

2. Navigate to the Automation Account: Once the account is created, navigate to it in the Azure portal. Under "Process Automation," click on "Runbooks" to manage your automation scripts[1][4].

3. Create a New Runbook: Click on "+ Create a runbook" and fill in the details:
- Name: Give your runbook a descriptive name, such as "StopIdleVM."
- Runbook type: Choose "PowerShell" as the runbook type.
- Description: Optionally add a description for clarity[1][9].

4. Add PowerShell Script: In the Runbook editor, paste a PowerShell script that checks the VM's status and shuts it down if necessary. Here is a basic example:

powershell
   param (
       [string]$ResourceGroupName,
       [string]$VMName
   )

   # Get the VM status
   $vm = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName

   if ($vm.PowerState -eq "VM running") {
       # Stop the VM if it is running
       Stop-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName -Force
       Write-Output "VM $VMName stopped."
   }
   

Click "Save" and then "Publish" to make the runbook available for execution[1][9].

5. Create a Schedule for the Runbook: To automate the shutdown process, you need to set up a schedule:
- In the Runbook, click on "Schedules" and then "+ Add a schedule."
- Choose "Create a new schedule" and specify the schedule details, such as start time and recurrence (e.g., daily or hourly).
- Link the schedule to your runbook by selecting the schedule and setting the required parameters (e.g., `ResourceGroupName` and `VMName`)[1][3].

6. Test the Runbook: Before relying on the schedule, test the runbook manually by clicking "Start" and verifying that it correctly shuts down the VM[9].

Citations:
[1] https://dev.to/rajeshkumaryadavdotcom/how-to-shutdown-your-azure-virtual-machine-automatically-when-idle-for-x-minute-a-step-by-step-guide-24jn
[2] https://github.com/tomasrudh/AutoShutdownSchedule
[3] https://community.dynamics.com/blogs/post/?postid=534937a9-ff48-489e-a2a9-3976a8a9f27f
[4] https://learn.microsoft.com/en-us/azure/automation/manage-runbooks
[5] https://thomasthornton.cloud/2020/05/26/creating-custom-runbooks-from-start-stop-vm-solution-for-specific-sets-of-vms-using-tags-for-sequenced-start-stop/
[6] https://learn.microsoft.com/en-us/azure/automation/automation-runbook-execution
[7] https://learn.microsoft.com/en-us/answers/questions/1526997/how-to-fix-azure-start-and-stop-vm-using-runbook-i
[8] https://stackoverflow.com/questions/32194613/azure-automation-vm-shutdown-runbook-not-working-on-new-vm
[9] https://www.alphabold.com/automate-the-vm-start-and-stop-process-using-powershell-runbook/