Last active
May 22, 2017 12:54
-
-
Save gravejester/82b70b1ce7bc7e2c3c18285e0f76fe5c to your computer and use it in GitHub Desktop.
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
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