Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

How to redeploy Azure VMs using command-line tools

·384 words·2 mins· 100 views · 5 likes ·
Azure CLI Azure Cloud Shell Azure PowerShell Get-AzSubscription

Today in this mini-post, I want to show you how to redeploy an Azure VM. This is normally a process that most people do from the Azure Portal, but it can also be done from the command line. As you know, the process of redeploying a VM consists of moving the VM to a new node within azure and turning it back on. When do you need to redeploy your VM? When the VM is in an inconsistent state and you do not have connectivity by RDP or SSH.

Important>

Important #

  • The ‘redeployment’ operation is not allowed in VMs that are deallocated or marked to be deallocated.
  • Please note that the dynamic IP addresses associated with the virtual network interface will be updated.
Azure PowerShell Workaround>

Azure PowerShell Workaround #

If you want to know how to install the PowerShell Azure module on your machine, check out this link. The simplest way to get started is to sign in interactively at the command line.

Connect-AzAccount

This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account. If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task, we will use the following commands:

Get-AzSubscription
Select-AzSubscription -Subscription "My Subscription"

Once you set your default subscription, you’re ready to start.

Set the variables>

Set the variables #

Here, we define the characteristics of our environment and the resource’s properties.

$resourceGroupName = 'RG-DEMO-NE'
$vmName = 'VM-DEMO-NE'

To redeploy the VM you should use the Set-AzVM cmdlet with the following syntax.

Set-AzVM `
    -Name $vmName `
    -ResourceGroupName $resourceGroupName `
    -Redeploy

Redeploy Azure VMs

Azure CLI Workaround>

Azure CLI Workaround #

In this case, we will use Azure Cloud Shell, a browser-based shell built into Azure Portal. This allows us to use the Azure command-line tools (Azure CLI and Azure PowerShell) directly from a browser. If you want to know more about Azure Cloud Shell, check out this link

resourceGroupName="RG-DEMO-NE"
vmName="VM-DEMO-NE"

To redeploy the Virtual Machine using Azure CLI you should the following command.

az vm redeploy \
-n $vmName \
-g $resourceGroupName

Thanks for reading my post. I hope you find it useful.

If you want to know more about how to troubleshoot Azure VMs, check out this link.