Number of Storage Accounts and Cores Consumed

As noted in my prior blog post on Tagging Azure Resource Groups with an Owner, our team is hitting the subscription limits for storage accounts and cores on Azure. Once we've added a created-by tag to the resource groups, we can now figure out who on the team is responsible for consuming all these storage accounts and cores.

I've written a Powershell script that will walk through all the resource groups in an Azure subscription and output the number of storage accounts and cores consumed in that resource group:

$rgNames = (Get-AzureRmResourceGroup).ResourceGroupName

foreach ($rgName in $rgNames)  
    $rg = Get-AzureRmResourceGroup -Name $rgName

    # owner
    $owner = "Unknown"
    $tags = $rg.Tags
    if ($tags)
        $owner = $tags["CREATED-BY"]

    $resources = Find-AzureRmResource -ResourceGroupNameEquals $rgName

    # storage account
    $numSA = $resources | Where {$_.ResourceType -eq "Microsoft.Storage/storageAccounts" } | measure  | Select -ExpandProperty Count

    # cores
    $numCores = 0

    #    vm
    $vmNames = $resources | Where {$_.ResourceType -eq "Microsoft.Compute/virtualMachines" } | Select -ExpandProperty Name
    foreach ($vmName in $vmNames) {
        $size = (Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName).HardwareProfile.VmSize
        $numCores += Get-AzureRmVmSize -location $rg.Location | ?{ $ -eq $size } | Select -ExpandProperty NumberOfCores

    #    vmss
    $vmssNames = $vmNames = $resources | Where {$_.ResourceType -eq "Microsoft.Compute/virtualMachineScaleSets" } | Select -ExpandProperty Name
    foreach ($vmssName in $vmssNames) {
        $vmss = Get-AzureRmVmss -ResourceGroupName $rgName -VMScaleSetName $vmssName
        $size = $vmss.Sku.Name
        $numCores += (Get-AzureRmVmSize -location $rg.Location | ?{ $ -eq $size } | Select -ExpandProperty NumberOfCores) * $vmss.Sku.Capacity

    Write-Host "$rgName, $owner, $numSA, $numCores"

The output can then be put into Excel for further data visualization. We plan on capturing this data in a monthly email to the team as a friendly reminder of what resources they are using and to delete them when they are done with them.