PowerShell Quick Tip: Validate the status of a virtual machine

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.

Validate virtual machine status

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.

Validate virtual machine status

I added the function in my GitHub, and you can access it here.

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top