schedule reboot

Daniel Houtmann 20 Reputation points
2025-06-16T11:40:21.7+00:00

How to create a policy for a scheduled reboot on a group of machine ?

Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
1,014 questions
0 comments No comments
{count} votes

Accepted answer
  1. Naveena Patlolla 3,605 Reputation points Microsoft External Staff Moderator
    2025-06-16T15:17:19.5733333+00:00

    Hi Daniel Houtmann

    The policy restricts access by specifying allowed or denied actions. However, if you want to schedule a machine reboot, consider using an Automation Account.
    1)Create an Automation account and Runbook

    2)Connect to Azure with system-assigned managed identity (provide contributor rights to Managed identity).
    Below is the PowerShell script:

     $AzureContext = (Connect-AzAccount -Identity).context
     # set and store context
     $AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
     # Connect to Azure (if not already connected) 
    #Connect-AzAccount 
    Select-AzSubscription -Subscription "SubscrpionName" 
    # Define the tag key and value 
    $TagKey = "Environment" 
    $TagValue = "dev-Autoshutdown" 
    # Get all VMs that have the specified tag value 
    $VMs = Get-AzVM | Where-Object { $_.Tags[$TagKey] -eq $TagValue } 
    # Stop the VMs 
    foreach ($VM in $VMs) {     
    Write-Host "Stopping VM: $($VM.Name) in Resource Group: $($VM.ResourceGroupName)"     
    #For Stop use the Below Command in the Stop Dev VMs Run book     
    Stop-AzVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName -Force      
    #For start use the Below Command in the Stop Dev VMs Run book     
    #Start-AzVM -Name $VM.Name -ResourceGroupName $VM.ResourceGroupName 
    } 
    Write-Host "All dev-test tagged VMs have been powered off."
    

    Please let me know if you face any challenge here, I can help you to resolve this issue further

    Provide your valuable Comments.

    Please do not forget to "Accept the answer” and “upvote it” wherever the information provided helps you, this can be beneficial to other community members.it would be greatly appreciated and helpful to others.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.