Skip to content

Instantly share code, notes, and snippets.

@pazdedav
Created November 15, 2019 09:49
Show Gist options
  • Save pazdedav/9315a0dab8e525af8a67fe02cfc738da to your computer and use it in GitHub Desktop.
Save pazdedav/9315a0dab8e525af8a67fe02cfc738da to your computer and use it in GitHub Desktop.
TSI Mini-Hack
# Retrieve automation variables
$costTagName = Get-AutomationVariable -Name 'tagName' -ErrorAction SilentlyContinue
$costTagValue = Get-AutomationVariable -Name 'tagValue' -ErrorAction SilentlyContinue
# Login to Azure
$RunAsConnection = Get-AutomationConnection -Name "AzureRunAsConnection"
Write-Verbose "Logging in to Azure..." -Verbose
Add-AzAccount -ServicePrincipal -TenantId $RunAsConnection.TenantId -ApplicationId $RunAsConnection.ApplicationId -CertificateThumbprint $RunAsConnection.CertificateThumbprint
Select-AzSubscription -SubscriptionId $RunAsConnection.SubscriptionID
# Retrieve a collection of all RGs in a subscription
$resourceGroups=Get-AzResourceGroup
Write-Output "Number of RGs: $resourceGroups.Count"
# Main loop
ForEach ($resourceGroup in $resourceGroups) {
$tags = (Get-AzResourceGroup -Name $resourceGroup).Tags
$tags.Add($costTagName, $costTagValue)
Write-Output "Adding cost tag to resource group: $resourceGroup.Name"
Set-AzResourceGroup -Tag $tags -Name $resourceGroup
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment