Skip to content

Instantly share code, notes, and snippets.

@sjwaight
Created June 12, 2018 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjwaight/66a2b08575351aac70569ca743ffd431 to your computer and use it in GitHub Desktop.
Save sjwaight/66a2b08575351aac70569ca743ffd431 to your computer and use it in GitHub Desktop.
Shows how you can enumerate Tags on Azure Resource Groups and write them to a Storage Table.
Import-Module AzureRmStorageTable
$runDateTime = Get-Date -Format "yyyy-MM-ddTHH\:mm\:ss.fffffffzzz"
$resourceGroup = "STORAGE_RESOURCE_GROUP"
$storageAccount = "STORAGE_ACCOUNT_FOR_TABLE"
$tableName = "STORAGE_ACCOUNT_TABLE_NAME"
# Connect to Storage Account (we assume you have already connected to a Subscription using Login-AzureRmAccount)
$saContext = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccount).Context
$table = Get-AzureStorageTable -Name $tableName -Context $saContext
# Read all Resource Groups in current subscription
$rgs = Get-AzureRmResourceGroup
ForEach ($rg in $rgs) {
# Partition Key
# Strip leading character (/) and then replace remaining / with - as / illegal in partiton key
$partitionKey = $rg.ResourceId.Substring(1).Replace("/","-");
# Read all tags on the Resource Group
$tags = $rg.Tags;
# Tag entries
if ($tags -ne $null)
{
ForEach($tag in $tags.GetEnumerator())
{
$rowKey = $tag.Key + "-" + $runDateTime;
# Could inline this hashtable, but this makes it clear what we are recording
$trackValues = @{}
$trackValues.Add("ResoureGroupName",$rg.ResourceGroupName);
$trackValues.Add("TagName",$tag.Key);
$trackValues.Add("TagValue",$tag.Value);
# Write new row to Table Storage. Property will be expanded to columns in table
Add-StorageTableRow -table $table -partitionKey $partitionKey -rowKey $rowKey -property $trackValues
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment