How to remotely stop, deallocate or start a Virtual Machine located inside Microsoft Azure?
The same can be replicated when your remote PCs are located in the cloud, and your Admin has connected them to your on-premises network through Azure Active Directory (Azure AD gives you the reliability and scalability you need with identity services that work with your on-premises, cloud, or hybrid environment.)
The issue I am addressing in this post is related to the following user story: As a developer or IT professional, I want to Start, stop and deallocate my Azure Virtual Machine, without necessarily having to login to my Azure portal.
Install PowerShell: Windows PowerShell comes installed by default in every Windows, starting with Windows 7 SP1 and Windows Server 2008 R2 SP1. If you are interested in PowerShell 6 and later, you need to install PowerShell Core instead of Windows PowerShell. For that, see Installing PowerShell Core on Windows.
Setup Azure CLI on your local machine:
For instructions on how to install Azure CLI, please read the previous post What is Azure CLI?
I am writing this with Windows users in mind and that is why in step one, I mentioned the existence of PowerShell.
Now, as I said before, you can use the normal Windows CMD, but I recommend using PowerShell instead as this offers some auto-completion when typing commands, and believe me, you will appreciate this handy feature.
Sart an Azure Session on your local machine: (This is so that you don't need to type username and password all the time on the same machine, I included these steps in the final script just in case you move from machine to machine, and it does not hurt the script in any way): Step 1: Type "az Login" at the PowerShell prompt
Step 2: Enter your user name and password to connect to Azure
Stop a running Virtual machine inside Azure:
Just put this PowerShell script in a file on your desktop or somewhere accessible (You must first update the parameters with you own values, and run the commands one by one to see how they perform). From now on, just double-click the script to shut down and deallocate your machine (I cannot over-emphasize how much money this will save you. You can also set it up in a scheduler on your local machine machine).
- Start a stopped/Deallocated virtual Machine in Azure Just put this PowerShell script in a file on your desktop or somewhere accessible (You must first update the parameters with your own values, and run the commands one by one to see how they perform).
Once done, just place the .ps1 file on your desktop. Anytime, you want to work with your Azure Virtual machine, just double click and in 90 seconds or so, you will be presented with your remote desktop login.