Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET
Microsoft Azure

How to Force an Azure VM Shutdown

In this easy Ask the Admin, I’ll show you how to force an Azure virtual machine to shut down.

Azure VMs are generally reliable, but there can be occasions when they get stuck during the start process and cannot be further controlled using the web-based management portal. In this article, I’ll show you how to use PowerShell to resolve this problem so that you can have another, hopefully successful attempt, at starting your virtual machine.

A Common Reason to Force an Azure VM to Shut Down

There are different reasons why you might want to force an Azure VM to shut down, but one of the most common is if a VM hangs when starting. Before you run the commands below, you will need to have Microsoft Azure PowerShell installed on your management PC, and make a secure connection to your Azure subscription. For more information on managing Azure using PowerShell, see Setup Windows Azure PowerShell Management on the Petri IT Knowledgebase.

Once you’ve got Microsoft Azure PowerShell installed, and a publish settings file downloaded and imported for your Azure subscription, open a PowerShell prompt and select the Azure subscription that you want to use in the current session:

Sponsored Content

Say Goodbye to Traditional PC Lifecycle Management

Traditional IT tools, including Microsoft SCCM, Ghost Solution Suite, and KACE, often require considerable custom configurations by T3 technicians (an expensive and often elusive IT resource) to enable management of a hybrid onsite + remote workforce. In many cases, even with the best resources, organizations are finding that these on-premise tools simply cannot support remote endpoints consistently and reliably due to infrastructure limitations.

​Select-AzureSubscription –SubscriptionName Pay-As-You-Go

If you’re not sure what Azure subscriptions have been installed on the local management PC, run the command below and look for the SubscriptionName field in the results:

Get-AzureSubscription

You can confirm the subscription set in the current PowerShell session using Get-AzureSubscription:

​Get-AzureSubscription –Current

Stop an Azure Virtual Machine

Now that PowerShell is configured and the Azure subscription set in the current session, let’s look at forcing a shutdown. Using the Stop-AzureVM cmdlet as shown below, specify the VM’s name, contososrv1 in this example, and the related cloud service, which is also contososrv1:

​Stop-AzureVM –Name contososrv1 –ServiceName contososrv1 -Force

If you’re not sure of the cloud service name, use Get-AzureVM to list all the VMs in the subscription and return the cloud service names. You can pipe the results of the Get-AzureVM cmdlet to Where-Object, which will return information just about a specific virtual machine if you have a lot of VMs in the subscription:

Forcing an Azure VM to shut down using PowerShell (Image Credit: Russell Smith)
Forcing an Azure VM to shut down using PowerShell (Image Credit: Russell Smith)
​Get-AzureVM | where { $_.Name –eq 'contososrv1' }

Alternatively, you can pipe the results of the above command line straight to the Stop-AzureVM cmdlet, without manually typing the cloud service name:

​Get-AzureVM | where { $_.Name –eq 'contososrv1' } | Stop-AzureVM -Force

You’ll notice in the figure that the VM’s status is ReadyRole, which means that it has successfully booted. If your VM is hung during startup, the status might be RoleStateUnknown.

Once the VM has actually shut down the status will be StopDeallocated, at which point you can either return to the web-based management portal and attempt to start it again, or use PowerShell as shown here:

​Get-AzureVM | where { $_.Name –eq 'contososrv1' } | Start-AzureVM

For more information on managing Azure VMs using PowerShell, see Stop or Start VMs in an Windows Azure Subscription Using PowerShell Loop and Stop Microsoft Azure Virtual Machines in Parallel using PowerShell on the Petri IT Knowledgebase.

Related Topics:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

Register
Comments (0)

Leave a Reply

IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.