More often than you might expect, you may have to validate if any given virtual machine is running or stopped (deallocated) in Microsoft Azure. I wrote a function where we need to provide the VM name and the desired status that we want. The function will be running until the VM does not return the desired condition.
The code is simple, and we use it to keep retrieving the information of the VM until we get the desired state. When we get the desired state, then the function returns $True.
In the example below, we started running the function against the VM apvm002, and manually we stopped the VM. After three checks, and when the VM was deallocated, then the $True value was returned. If you want to remove the current information about the VM status, you can always remove the Else{} statement from the script.
I added the function in my GitHub, and you can access it here.
More PowerShell Basics articles
- Working with dates in PowerShell revisited
- PowerShell regular expressions: Making string evaluation easier
- PowerShell concatenation: How to use this powerful feature
- What does a question mark mean in PowerShell commands?
- Using Group Policy settings to enforce PowerShell execution policies
More Quick Tips articles
- Fixing Azure Key Vault when moving to a different tenant
- Restore Azure Key Vault using just two PowerShell cmdlets
- This overlooked feature in Visual Studio Code can speed release time
- Enabling Front Door managed certificates in Azure: Status update
- How to quickly check the status of all your Azure services