Friday, June 9, 2023

How to start, restart, stop or delete multiple Azure VMs using Azure Powershell and Azure CLI

To start, restart, stop, or delete multiple Azure virtual machines (VMs), you can use Azure PowerShell or the Azure CLI. Here are the steps using both methods:

Using Azure PowerShell:

  1. Install Azure PowerShell: If you haven't already, install the Azure PowerShell module on your local machine. You can follow the instructions provided by Microsoft to install Azure PowerShell: https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-12.0.0

  2. Connect to your Azure account: Open PowerShell and run the following command to sign in to your Azure account:

    powershell
    Connect-AzAccount
  3. Select the Azure subscription: If you have multiple Azure subscriptions, use the following command to select the appropriate subscription:

    powershell
    Set-AzContext -SubscriptionId <SubscriptionId>
  4. Start, restart, stop, or delete VMs: Use the appropriate commands to manage your VMs. For example:

    • To start VMs:
      powershell
      Start-AzVM -ResourceGroupName <ResourceGroupName> -Name <VM1Name>, <VM2Name>, ...
    • To restart VMs:
      powershell
      Restart-AzVM -ResourceGroupName <ResourceGroupName> -Name <VM1Name>, <VM2Name>, ...
    • To stop (deallocate) VMs:
      powershell
      Stop-AzVM -ResourceGroupName <ResourceGroupName> -Name <VM1Name>, <VM2Name>, ...
    • To delete VMs:
      powershell
      Remove-AzVM -ResourceGroupName <ResourceGroupName> -Name <VM1Name>, <VM2Name>, ...

Using Azure CLI:

  1. Install Azure CLI: If you haven't already, install the Azure CLI on your local machine. Follow the instructions provided by Microsoft to install Azure CLI: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli

  2. Sign in to your Azure account: Open the command prompt or terminal and run the following command to sign in to your Azure account:

    bash
    az login
  3. Select the Azure subscription: If you have multiple Azure subscriptions, use the following command to select the appropriate subscription:

    bash
    az account set --subscription <SubscriptionId>
  4. Start, restart, stop, or delete VMs: Use the appropriate commands to manage your VMs. For example:

    • To start VMs:
      bash
      az vm start --resource-group <ResourceGroupName> --name <VM1Name> <VM2Name> ...
    • To restart VMs:
      bash
      az vm restart --resource-group <ResourceGroupName> --name <VM1Name> <VM2Name> ...
    • To stop (deallocate) VMs:
      bash
      az vm stop --resource-group <ResourceGroupName> --name <VM1Name> <VM2Name> ...
    • To delete VMs:
      bash
      az vm delete --resource-group <ResourceGroupName> --name <VM1Name> <VM2Name> ...

Remember to replace <ResourceGroupName>, <VM1Name>, <VM2Name>, and <SubscriptionId> with the actual names and IDs relevant to your Azure environment.

Both Azure PowerShell and Azure CLI provide powerful automation capabilities, so you can script or loop through a list of VMs to perform bulk operations efficiently.

No comments:

Azure Cloud shell commands cheetsheet

  Certainly! Here's a cheat sheet of commonly used Azure Cloud Shell commands: Azure Account az login : Authenticate and log in to your ...