Shuttinmg down VM'S per resource group.

Nahum Eliash 41 Reputation points
2022-12-22T10:55:48.647+00:00

Hi Everyone,

Is there's a way to shut down VM machines per Resource groups?
(we have about 40 machines some of them are Windows, most of them are Linux development machines)

What would you recommend me to do instead of configuring each VM separately (using azure Automation tasks in the GUI).

Thanks in advance.
Best regards.

Nahum

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,333 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ramya Harinarthini_MSFT 5,346 Reputation points Microsoft Employee
    2022-12-22T14:33:32.657+00:00

    @Nahum Eliash Welcome to Microsoft Q&A, thank you for posting your here!

    You can deploy a feature Start/Stop VMs v2 to an Azure subscription. The deployment is initiated from the Start/Stop VMs v2 GitHub organization. While this feature is intended to manage all of your VMs in your subscription across all resource groups from a single deployment within the subscription, you can also install another instance of it based on the operations model or requirements of your organization. It also can be configured to centrally manage VMs across multiple subscriptions.To simplify management and removal, we recommend you deploy Start/Stop VMs v2 to a dedicated resource group.

    To manage the automation method to control the start and stop of your VMs, you can configure schedule logic apps based on your requirements.

    Hope this helps!
    Kindly let us know if the above helps or you need further assistance on this issue.

    ---------------------------------------------------------------------------------------------------------------------------

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

    2 people found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Bjoern Peters 8,901 Reputation points
    2022-12-22T11:02:11.243+00:00

    Hi @Nahum Eliash

    I would recommend using PowerShell in an automation task...

    Get-AzVm -ResourceGroupName 'MyResourceGroup' | Stop-AzVM  
    

    I hope my answer is helpful to you,

    Your
    Bjoern Peters

    If the reply was helpful, please upvote and/or accept it as an answer, as this helps others in the community with similar questions. Thanks!

    1 person found this answer helpful.

  2. dkrishnaveni-MSFT 1,956 Reputation points Microsoft Employee
    2022-12-30T16:55:17.77+00:00
    0 comments No comments

  3. srbhatta-MSFT 8,576 Reputation points Microsoft Employee
    2023-01-03T09:27:02.13+00:00

    Hello @Nahum Eliash ,
    Yes, as far as my understanding goes, you can use Azure Functions to start and stop all Azure Virtual Machines (VMs) that are part of the same resource group. Here are the steps to do this:

    • Create an Azure Function with a timer trigger. You can set the timer to run on a schedule, such as every day at a certain time.
    • In the function code, use the Azure Management Libraries for .NET to retrieve a list of VMs in the specified resource group.
    • Loop through the list of VMs and start or stop them as needed.
    • Deploy the function to Azure.

    Here is some sample code that demonstrates how to start all VMs in a resource group using an Azure Function:

    using Microsoft.Azure.Management.Compute.Fluent;  
    using Microsoft.Azure.Management.Fluent;  
    using Microsoft.Azure.Management.ResourceManager.Fluent;  
    using Microsoft.Azure.Management.ResourceManager.Fluent.Core;  
    using Microsoft.Extensions.Logging;  
      
    public static async Task Run(TimerInfo myTimer, ILogger log)  
    {  
        var azure = Azure  
            .Configure()  
            .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)  
            .Authenticate(credentials)  
            .WithDefaultSubscription();  
      
        var resourceGroupName = "myResourceGroup";  
      
        var vms = azure.VirtualMachines.ListByResourceGroup(resourceGroupName);  
        foreach (var vm in vms)  
        {  
            log.LogInformation($"Starting VM: {vm.Name}");  
            await vm.StartAsync();  
        }  
    }  
    

    This code uses the Azure Management Libraries for .NET to authenticate to Azure and retrieve a list of VMs in the specified resource group. It then loops through the list of VMs and starts them using the StartAsync method.
    Do let me know if this helps.

    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.