Created
November 15, 2019 09:49
-
-
Save pazdedav/9315a0dab8e525af8a67fe02cfc738da to your computer and use it in GitHub Desktop.
TSI Mini-Hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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