Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.