Collecting Information about Azure Resource Groups with PowerShell
I am a huge fan of Azure Graph Explorer and, more specifically, the Az.ResourceGraph module. My favorite feature is not having to switch subscriptions in order to query across them. For those that may not know, the Azure Graph Explorer API is a RESTful web API that allows you to query Azure cloud resources. Another feature is that is is FAST. Sacrificing editable properties for speed, anything pulled using the Graph API is read-only. Sometimes, that is all you need. This feature can be extremely helpful for reporting on your Azure landscape among other uses. You can access Azure Graph Explorer through the portal, use the documented REST API calls, or use PoSH through the use of the Az.ResourceGraph module combined with the Search-Az Graph cmdlet. Here is the Microsoft documentation for Search-AzGraph. One thing that I noticed, however, is the that while you can do some pretty neat bottom up querying (resource -> resource group, for example), querying in the opposite direction is not possible. For instance, you cannot query for a resource group and its properties itself. I went to Graph Explorer and, sure enough, there is no table for resource group so it makes sense why such queries would not work in the first place. In any case, I needed to get all resource groups across subscriptions and get a specific tag from each one so I proceeded to use good old reliable PoSH. This script will accept a list of subscription names and for each one it will grab a list of resource groups, create a custom PSObject to collect the resource group name and desired tag, and output this object to a csv file. Areas for customization include adding and removing resource group information to be collected, the location of the csv file and the naming of the csv file. You should be authenticated to your Azure tenant before running.
#The purpose of this script is to easily obtain information about resource groups
#The purpose of this script is to easily obtain information about resource groups
Param ( [Parameter(Mandatory = $true)] [string[]] $subscriptionNames )
$ErrorActionPreference = "Stop"
foreach ($subscriptionName in $subscriptionNames) { try { Select-AzSubscription -Subscription $subscriptionName } catch { Write-Error "Unable to get Subscription: $($subscriptionName)" }
try { $RGs = Get-AzResourceGroup } catch { Write-Error "Unable to retrieve Resource Groups in subscription: $($subscriptionName)" }
$output = @()
foreach ($RG in $RGs) { $outputObject = New-Object PSObject -Property @{ "Name of Column" = $RG.<property> #insert property here #add more columns, if desired }
$output += $outputObject }
$output | Export-Csv ".\$($subscriptionName)-NotificationDLs.csv" -NoTypeInformation }