Scheduling Reboot for Azure Virtual Machines using Automation Account

This documentation offers a comprehensive guide with step-by-step instructions on leveraging an Azure Automation Account to schedule reboots for your Azure Virtual Machines (VMs). The automation of reboots is crucial for maintaining the health and ensuring the timely updates of your VMs. Since Microsoft's Azure platform doesn't offer native options for scheduled reboots, we employ the use of Runbooks and Automation Accounts as an effective workaround..

Prerequisites

Before you begin, make sure you have the following prerequisites:

  1. An Azure subscription: You need an active Azure subscription to create and manage Azure resources.
  2. An Azure Virtual Machine: You should have at least one Azure VM for which you want to schedule reboots.

Step by Step:

  1. Create an Automation account:1.1 In the search tab, enter 'Automation account,' then proceed to select the relevant subscription, and resource group, and make certain that you have chosen the accurate region.


1.2 Choose the Managed Identity as 'System assigned'.

1.3 Depending on your network configuration, you can opt for 'Public network' if you require public access, or choose 'Private access' if you need to access resources privately. Afterward, select the appropriate Virtual Network (VNet).

1.4 Select Create.

1.5 After creating the Automation account, navigate to the 'Identity' section in the left pane.

Automation account - Identity

1.6 Next, select 'Azure Role Assignment,' and within the assignment configuration, choose the 'Scope' as 'Subscription.' For 'Subscription,' select the subscription where your VMs are located, and assign the 'Contributor' role.





1.7 Access the Automation account you've created, and in the left pane, click on 'Runbook.' Then, proceed to select 'Create a runbook.'


1.8 Specify your desired name, and under the 'Runbook type' dropdown menu, choose 'PowerShell Workflow' and then Create.

1.7 Access the newly created Runbook, and select 'Edit.' Then, opt for 'Edit in Portal.'

1.8 Here's the modified code that you can copy and paste into your runbook after replacing the placeholders for the resource group name and VM name. You can repeat the line for each VM you want to restart:

workflow demo-reboot-runbook
{
    InlineScript
    {
        Connect-AzAccount -Identity
Restart-AzVM -ResourceGroupName "Resource Group name" -Name "VM1 name"
Restart-AzVM -ResourceGroupName "Resource Group name" -Name "VM2 name"
Restart-AzVM -ResourceGroupName "Resource Group name" -Name "VM3 name"
# Add more lines as needed for additional VMs 
    }
}        

1.9 You can now initiate a test of the Runbook by clicking on the 'Test Pane' and waiting for the code to finish execution.

You can now initiate a test of the Runbook by clicking on the 'Test Pane' and waiting for the code to finish execution.

  1. Scheduling the Reboot2.1 Within the Automation account, go to the left-hand pane choose 'Schedules', and choose Once if you need to reboot the VMs at a specific time.

2.2 Alternatively, you can opt for 'Recurring' and then specify the desired interval by selecting the preferred day and time.


2.3 Next, we'll link the schedule to our Runbook. To do this, navigate to the Runbook, and in the left pane, select 'Schedules.


2.4 Choose 'Link a schedule to your runbook,' leave the second option unchanged, and proceed to click 'Create.

2.5

2.6 or the final step, you can verify the behavior by checking the VM's activity log, as illustrated below.

Thank you for completing the article. If you have any questions or need further assistance, please feel free to reach me at the contact number provided in my signature.



Best regards,

Mohamed Hussien Rabea

Sr. Microsoft Consultant

+201023113355


Paras Kumar

Team Leader – System Administration at International Accreditation New Zealand

5 个月

Hi Mohammad. Is there a way to reboot an azure connected machine? I have an on prem windows server and I would like to schedule a reboot for it. Thanks.

回复
Aishwarya Jagannathan

Cloud Architect | Enterprise Landing Zone | Workload Migration | SRE | Infrastructure Automation

1 年

Hello. I read your article. Its well explained. I'm working on similar automation solution. Even, I hard-coded the inputs in few of the solutions. But, have you tried passing multiple resource group or subscriptions input via parameters? I would love to get your input on the same.

回复
Aya Eid

Founder of Tosn Courses and trainer on Udemy

1 年

???????? ????? ????? ????

回复
Yasmine Shehab

Modern Workplace Specialist at Microsoft

1 年

Well done rabea

回复
Stanley Russel

??? Engineer & Manufacturer ?? | Internet Bonding routers to Video Servers | Network equipment production | ISP Independent IP address provider | Customized Packet level Encryption & Security ?? | On-premises Cloud ?

1 年

Your article on scheduling VM reboots in Azure using Runbooks and Automation Accounts is a valuable resource for Azure administrators. I appreciate the step-by-step guide you've provided, which makes it accessible even for those less experienced in Azure automation.I'm intrigued by the potential scalability of this approach. Could you shed light on how this method handles a large number of VMs efficiently? Are there any considerations for managing hundreds or thousands of VMs with scheduled reboots to avoid overloading the system or causing disruptions?Additionally, when it comes to the activity log verification, are there specific patterns or indicators administrators should look for to ensure that the reboots have been executed as intended without any hiccups?

要查看或添加评论,请登录

Mohamed Hussien的更多文章

社区洞察

其他会员也浏览了