Skip to content

Instantly share code, notes, and snippets.

@gravejester
Last active May 22, 2017 12:54
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 gravejester/82b70b1ce7bc7e2c3c18285e0f76fe5c to your computer and use it in GitHub Desktop.
Save gravejester/82b70b1ce7bc7e2c3c18285e0f76fe5c to your computer and use it in GitHub Desktop.
function Update-AzureRmTag {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 0)]
[string[]] $ResourceId,
[Parameter()]
[hashtable] $Tags = [hashtable]::new()
)
BEGIN {}
PROCESS {
foreach ($resource in $ResourceId) {
try {
$thisResource = Get-AzureRmResource -ResourceId $resource
Write-Verbose "Processing $($thisResource.Name)"
if ([bool]($thisResource.Tags.GetEnumerator().Where({$_.Name -notlike '*hidden-link*'}))) {
if ($thisResource.Tags.Count -ne 0) {
[hashtable]$currentTags = $thisResource.Tags
[hashtable]$newTags = $thisResource.Tags + $Tags
}
else {
[hashtable]$newTags = $Tags
}
$result = Set-AzureRmResource -ResourceId $thisResource.ResourceId -Tag $newTags -ErrorAction Stop -Force
}
else {
Write-Verbose 'hidden-link found - skipping'
}
}
catch {
Write-Warning "[$($thisResource.Name)] $($_.Exception.Message)"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment