How do setup automated tasks for my VM

Billy Van Wyk (PEC) 20 Reputation points
2025-01-09T14:31:07.76+00:00

I would to automate the power on and off of my VM on a daily basis to save costs

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,288 questions
Azure Automanage
Azure Automanage
An Azure service that simplifies and optimizes the management of virtual machines with automated operations.
5 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andrew Coughlin 160 Reputation points Microsoft Employee
    2025-01-09T15:40:06.41+00:00

    Hello Billy,

    Welcome to the Microsoft Q&A Community! For the automation of powering on and off of virtual machines, I would take a look at https://learn.microsoft.com/en-us/azure/azure-functions/start-stop-vms/overview. Also, when it comes to saving cost, there are some additional ways to save as well for example:

    -Azure Advisor Cost Recommendations: https://learn.microsoft.com/en-us/azure/advisor/advisor-reference-cost-recommendations

    -Reserved Instances: https://azure.microsoft.com/en-us/pricing/reservations

    -Savings Plans: https://azure.microsoft.com/en-us/pricing/offers/savings-plan-compute

    0 comments No comments

  2. Ashok Gandhi Kotnana 1,855 Reputation points Microsoft Vendor
    2025-01-10T05:30:46.76+00:00

    Hi @Billy Van Wyk (PEC) ,

    Welcome to Microsoft Q&A Forum, thank you for posting your query here!

    Let me provide you two solutions here

    Solution 1: If we have limited number of VM where you just go the VM and perform this

    Solution 2: Let us say if you have 100's of VM's solution 2 will make use using a script

    Please choose above two solutions depending up on your requirement

    Solution1 : Directly go the VM Blade go to tasks.

    User's image

    Add two tasks on is for Stop VM and one is for Start VMUser's imageClick on Sign in provide your credentials

    User's imageLater you can see it is connected

    User's image

    Schedule your stop and start activity depends up on your requirement

    User's image

    Solution 2:

    Create an Automation account

    Refer: https://learn.microsoft.com/en-us/azure/automation/quickstarts/create-azure-automation-account-portal

    Create a runbook

    Refer: https://learn.microsoft.com/en-us/azure/automation/learn/automation-tutorial-runbook-textual

    Later enable managed identity to keep on

    Refer: https://learn.microsoft.com/en-us/azure/automation/learn/automation-tutorial-runbook-textual

    Follow the below screenshots

    User's image

    Add role assignment

    User's image

    Add role Assignment and provide Contributor access to the automation account or manage identityUser's image

    Below script will stop the VM

    User's image Below script will Start the VMUser's image

    Code Added below: -

    #Connect to Azure with system-assigned managed identity
     
    $AzureContext = (Connect-AzAccount -Identity).context
     
    # set and store context
     
    $AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
    
    $vmlist = @("testtest", "myVm")
    foreach ($vm in $vmlist) {
    
    $rgname= get-azvm | where {$_.name -eq "$vm"}
    
    Write-Host $rgname.ResourceGroupName
    
    
        Stop-AzVM -Name $vm -ResourceGroupName $rgname.ResourceGroupName -Force
    }
    
    
    #Connect to Azure with system-assigned managed identity
     
    $AzureContext = (Connect-AzAccount -Identity).context
     
    # set and store context
     
    $AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
    
    $vmlist = @("testtest", "myVm")
    foreach ($vm in $vmlist) {
    
    $rgname= get-azvm | where {$_.name -eq "$vm"}
    
    Write-Host $rgname.ResourceGroupName
    
    
        Start-AzVM -Name $vm -ResourceGroupName $rgname.ResourceGroupName
    }
    
    

    let us know if you have any further queries. I’m happy to assist you further.  

    Please provide your valuable comments User's image

    Please do not forget to "Accept the answer” wherever the information provided helps you, this can be beneficial to other community members.

    0 comments No comments

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.