AzureRm PowerShell Commands for scripting and management

This post will be short and sweet, but I wanted to write a quick post on some cool commands you can use for your scripts / to manage your Azure subscription. I recently found myself wanting to pull a list of all VMs running in my Azure subscription, but the way to do this using Azure Resource Management is slightly different from Azure Service Management.

Note: This will only pull a list of your Azure RM VMs.

So, to explain the above, I used variables for the resource groups, the vms and status, and then stored the results in an object. The trick to easily format data in PowerShell is to parse the data into an object. I used the ‘Write-Host‘ command to push the output of the created object in table format to the pipeline. I am able to do this easily (especially one with only 3 properties) because of how PowerShell’s ‘Format-Table’ cmdlet works with objects.

Now, you might be wondering why you couldn’t just use the ‘Get-AzureRmVM’ command and then pipe to ‘Select-Object’ and the answer is there is no longer an object for the running status information. Here is a current list of the objects you can use with that command:

Notably, the ‘Statuses’ and ‘StatusCode’ objects do not display the running status of the VM.

Now, if you want to quickly get a list of your resource groups and other useful information about them you can use the following code:

If you have any other commands or code snippets you think others would find helpful, please feel free to leave a comment.

Reminder: All of my posts are provided "AS IS", imply no warranties, and confer no rights or special privileges. Use of included postings, code samples and other works are subject to the terms specified at Microsoft. For more information, click here.

Leave a Reply

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