I have a small script that will report Azure SQL Databases on 1 server. I’d like to be able to loop through a list of multiple subscriptions, multiple resource groups and list all the servers and databases. I’m assuming I need a nested loop, but unsure how to build the parameter list as shown below.
I have the following loop to return all subscriptions and all resource groups tied to that subscription. The only problem is it’s returning all the subscriptions, but only the resource groups for one subscription.
It’s returning the same. So it will return the 3 subscriptions, but all of the resource groups of one subscription.
I think I need a way to change the context in which the script is running, i.e., change the subscription during the loop but i’m still trying to figure that out.
I realized you definitely need to set the context to pull resource groups. However, the ‘Get-AzResourceGroup’ cmdlet doesn’t not need a Subscription value to retrieve information. I’d still like to loop through the ‘Set-AzContext’ cmdlet to set the subscription in order to pull the resource groups.
I wrote this to actually test the loop to pull SQL Servers, which works, but not truly dynamically.
I actually tried to do that after your first post. It should work, but it’s not. The 3 subscriptions are prd, dev, and qa. It’s only returning the resource groups under dev.
Sorry, that did work. The Set-AzureRMContext cmdlet is deprecated. I had to use Set-AzContext.
In order to retrieve the servers and database on those servers, do you recommend additional nested loops? The other cmdlets I need to use is one for the servers (Get-AzSqlServers) and then the databases (Get-AzSqlDatabase). The servers cmdlet requires a resource group name and the database cmdlet requires a resource group name and server name.
I’d just recommend to indent your code to make it easier readable.
This times 1000.
The number of times i’ve fixed bugs in my code by going back and doing proper formatting I can’t even begin to count. In the beginning it seemed silly to care, but once you get over just a few lines its vital.