14

I have an azure subscription and I'm trying to write a powershell script to automatically get a list of all the resources (VMs, Storage Accounts, Databases, etc) that I currently have in my subscription. Is there a way to do this using the azure management REST API or the Azure Cmdlets?

Oladunni Abiodun
  • 175
  • 1
  • 1
  • 7
  • For future searchers: `az resource list --subscription`, see https://learn.microsoft.com/en-us/cli/azure/resource?view=azure-cli-latest#az-resource-list – Slate Jan 13 '22 at 17:54

7 Answers7

10

If you are using the new Resource Manager model (introduced in 2014) you can use the following PowerShell script.

Login-AzureRmAccount
Get-AzureRmResource | Export-Csv "c:\Azure Resources.csv"

To use the Resource Manager PowerShell commands you will need the AzureRM PowerShell module (https://learn.microsoft.com/en-us/powershell/azure/install-azurerm-ps).

Install-Module AzureRM

For more information on the difference between Resource Manager and Classic models see, https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-deployment-model.

For users with multiple subscriptions: If you want to output the contents of multiple subscriptions then you will need to call Select-AzureRmSubscription to switch to another subscription before calling Get-AzureRmResource.

Andrew C
  • 1,231
  • 9
  • 15
4

I don't think there's just one function (or PS Cmdlet) to fetch all this information. However each of these can be fetched through both Windows Azure Service Management REST API as well as Window Azure PowerShell Cmdlets.

Windows Azure Service Management REST API: http://msdn.microsoft.com/en-us/library/windowsazure/ee460799.aspx. For example, if you want to list storage accounts in your subscription, you would use this: http://msdn.microsoft.com/en-us/library/windowsazure/ee460787.aspx

Windows Azure PowerShell Cmdlets: http://msdn.microsoft.com/en-us/library/jj554330.aspx. Again, if you want to list storage accounts in your subscription, you would use this: http://msdn.microsoft.com/en-us/library/dn205168.aspx.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • Thanks. Yeah that's how I'm currently doing it. Problem is there doesn't seem to be a way to get all my SQL databases for my subscription – Oladunni Abiodun Jul 11 '13 at 03:17
  • Is there any API which gives all the assets information ? – Jerry Aug 24 '16 at 05:03
  • I just provided an answer to your question: http://stackoverflow.com/questions/39102044/get-details-of-all-resources-in-my-azure-subscription-using-java-api/39115342#39115342. HTH. – Gaurav Mantri Aug 24 '16 at 05:50
3

Since you said PowerShell "preferably", I'm going to assume other options are still maybe useful? You can go to http://portal.azure.com, and click on the Menu icon (three horizontal lines), then All Resources. Then at the top of the page you can click Export to CSV and open that in Excel.

You have to take 30 seconds to do a little cleanup in Excel, but for what I'm trying to do right now, this was definitely the best & fastest solution. I hope it's useful to you (or someone else) too.

pbristow
  • 1,997
  • 4
  • 26
  • 46
3

well, You may update the version of your AzurePowershell and execute this command.

Get-AzureResource

In the output, You may check for "ResourceType". It has the information about the type of resource creatd on azure.

Rahul Mohan
  • 493
  • 3
  • 5
  • 18
3

I know it's already been answered however, I have found the Get-AzResource command easy to use and fetches all the resources from a particular subscription. Try using it with "ft" for clean text

Get-AzResource | ft

Screenshot

enter image description here

EZR
  • 63
  • 6
  • Thanks @EZR for updating the thread with new info! :) Please attach the image to the post, in case the url breaks in the future. – Carlos Garcia Mar 03 '20 at 20:43
  • If you want to specific subscription, run command firstly `Set-AzContext -Subscription "xxxx-xxxx-xxxx-xxxx"` – fkpwolf Oct 12 '22 at 01:57
2

Adding to @Gaurav's answer (and related to your comment about SQL database enumeration): You can enumerate all of your databases, on a per-server basis, in a few easy steps.

First, enumerate all of the SQL Database servers in your subscription:

enter image description here Then, for each server, create a connection context and enumerate the databases. Note that, with the Get-Credentials cmdlet, I was prompted to enter a username + password via a popup, which I don't show here. For demonstration purposes, I created a brand new server, with only a master database, to show what the output looks like:

enter image description here

David Makogon
  • 69,407
  • 21
  • 141
  • 189
  • Thanks. Yeah the problem I had with that approach was that this issue of popping up a credential prompt would prevent me from automating the process. Was wondering if there was a way around that. – Oladunni Abiodun Jul 11 '13 at 13:03
  • Have you seen [this blog post](http://gallery.technet.microsoft.com/scriptcenter/Execute-PowerShell-Script-38881dce) or [this post](http://www.powershellmagazine.com/2013/02/11/pstip-get-credential-at-the-command-line/) with a technique to avoid the popup? – David Makogon Jul 11 '13 at 13:15
  • Oh thanks. That could work. Does azure store the credentials for all the servers? Because when I go on the databases tab of the azure portal, it shows me a list of all databases across all servers without asking me to provide credentials for each server – Oladunni Abiodun Jul 11 '13 at 14:56
  • I wish you had copied the console output and put it in code tags instead of uploading images. Then we could use copy and paste. – Aran Mulholland Oct 12 '18 at 02:13
  • @AranMulholland - I posted this *over five years ago* (so old, that Azure was Windows Azure and SQL Database was SQL Azure). And I did the screenshots to show not only the commands but also how the output is rendered. And 5 years ago, there really wasn't much (if any) guidance on posting screenshots vs inline code. So yes, would've been helpful to post inline code. But odd to hear about this a half-decade later. – David Makogon Oct 13 '18 at 03:14
  • @DavidMakogon with some PowerShell commands, not that much has changed. I wonder sometimes if we should remove really old answers when the tech moves on, then again someone is still using the old tech somewhere.... – Aran Mulholland Oct 13 '18 at 09:40
2

This sample demonstrates how to automatically get a list of all the resources (VMs, Storage Accounts, Databases, App Services) and status via Powershell by certificate authentication.

https://gallery.technet.microsoft.com/Access-Azure-resource-data-ca9cc9f7

enter image description here

frank tan
  • 131
  • 1
  • 4