ManageTagsOnLoadBalancer.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
<#PSScriptInfo .VERSION 1.0 .GUID 986389ea-00c9-420d-b4b7-90c0313922d1 .AUTHOR AlexG .COMPANYNAME GooberWorks .COPYRIGHT GooberWorks (c) 2020 .TAGS Azure AzureRM Automation TAGS Tags Load Balancer NIC replace remove delete add tags ManageTagsOn .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .PRIVATEDATA #> <# .DESCRIPTION This Powershell script will help you manage TAGS on Azure Load Balancer Resource. While it is very easy to add and change tags using console, it is not so easy to delete a specific tag from a resource. The console also limits the number of resources you can update to 100. So if you need to change tags on more than 100 resources, then you'll need to rinse and repeat as often as neccessary This script will allow you to -Add, -Replace or -Remove a specific tag from any Load Balancer resource that matches a search criteria using parameters -ManageThisTag and -ManageThisTagValue. This script is part of a set of ManageTagsOn scripts. Please look for ManageTagsOnVM ManageTagsOnDisk ManageTagsOnNetworkCard ManageTagsOnLoadBalancer ManageTagsOnNetworkSecurityGroup ManageTagsOnAvailabilitySet ManageTagsOnRouteTable ManageTagsOnAutomationRunbooks .SYNOPSIS Add, Remove or Replace Tags on Load Balancer Resource in Azure .PARAMETER Remove The -Remove switch will tell the script to delete the tag specified by ManageThisTag and optionally if the value of the Tag matches the content of -ManageThisTagValue parameter .PARAMETER Replace The -Replace switch will tell the script to delete the tag specified by ManageThisTag and optionally if the value of the Tag matches -ManageThisTagValue parameter and replace it with a new Tag and Value specified by -NewTagKey and -NewTagValue parameters .PARAMETER ManageThisTag The name of the TAG to be used as a search criteria. Filters only those resources where this tag exists. .PARAMETER ManageThisTagValue The value of the TAG specified by ManageThisTag. This further filters only those resources where this tag exists and it value matches ManageThisTagValue. .PARAMETER FromGroup The name of the Resource Group to limit search scope. Filters only those Resources in the specific Resource Group. .PARAMETER Add The -Add switch works together with -NewTagKey and -NewTagValue parameters. This will make the script add a new tag NewTagKey with value of NewTagValue to the resource that match the search criteria .PARAMETER WhatIf Safe run of the script without any changes to preview changes that would be made. .EXAMPLE ManageTagsOnLoadBalancer.ps1 -Remove -ManageThisTag "Environment" -FromGroup "test-vm-servers-rg" .EXAMPLE ManageTagsOnLoadBalancer.ps1 -Replace -ManageThisTag "Environment" -ManageThisTagValue "Test" -NewTagKey "Environment" -NewTagValue "Development" .EXAMPLE ManageTagsOnLoadBalancer.ps1 -Add -ManageThisTag "Environment" -ManageThisTagValue "Test" -NewTagKey "Owner" -NewTagValue "Boston Development Team" .NOTES AUTHOR: Alexander Goldberg LASTEDIT: Feb 16, 2020 WEBSITE: www.alexgoldberg.com #> [cmdletbinding( DefaultParameterSetName='Remove' )] param( [Parameter(Mandatory=$true,ParameterSetName = "Remove")] [switch]$Remove, [Parameter(Mandatory=$true,ParameterSetName = "Replace")] [switch]$Replace, [Parameter(Mandatory=$true,ParameterSetName = "Remove")] [Parameter(Mandatory=$true,ParameterSetName = "Replace")] [Parameter(Mandatory=$false,ParameterSetName = "Add")] [string]$ManageThisTag, [Parameter(ParameterSetName = "Remove")] [Parameter(ParameterSetName = "Replace")] [Parameter(ParameterSetName = "Add")] [string]$ManageThisTagValue, [Parameter(Mandatory=$true,ParameterSetName = "Replace")] [Parameter(Mandatory=$true,ParameterSetName = "Add")] [string]$NewTagKey, [Parameter(Mandatory=$true,ParameterSetName = "Replace")] [Parameter(Mandatory=$true,ParameterSetName = "Add")] [string]$NewTagValue, [string]$FromGroup, [Parameter(ParameterSetName = "Add")] [switch]$Add, [switch]$WhatIf ) # Getting the list of LoadBalancer Resources. if ($PSCmdlet.MyInvocation.BoundParameters.Keys.Contains('ManageThisTagValue')) { if ($PSCmdlet.MyInvocation.BoundParameters.Keys.Contains('FromGroup')) { $Resources = Get-AzureRmLoadBalancer -ResourceGroupName $FromGroup | Where-Object -FilterScript {$_.Tag.Keys.Count -gt 0 -and $_.Tag.($ManageThisTag) -eq $ManageThisTagValue} } else { $Resources = Get-AzureRmLoadBalancer | Where-Object -FilterScript {$_.Tag.Keys.Count -gt 0 -and $_.Tag.($ManageThisTag) -eq $ManageThisTagValue} } } elseif ($PSCmdlet.MyInvocation.BoundParameters.Keys.Contains('ManageThisTag')) { if ($PSCmdlet.MyInvocation.BoundParameters.Keys.Contains('FromGroup')) { $Resources = Get-AzureRmLoadBalancer -ResourceGroupName $FromGroup | Where-Object -FilterScript {$_.Tag.Keys.Count -gt 0 -and $_.Tag.ContainsKey($ManageThisTag)} } else { $Resources = Get-AzureRmLoadBalancer | Where-Object -FilterScript {$_.Tag.Keys.Count -gt 0 -and $_.Tag.ContainsKey($ManageThisTag)} } } else { if ($PSCmdlet.MyInvocation.BoundParameters.Keys.Contains('FromGroup')) { $Resources = Get-AzureRmLoadBalancer -ResourceGroupName $FromGroup } else { $Resources = Get-AzureRmLoadBalancer } } if ($null -ne $Resources) { foreach ($Resource in $Resources) { Write-Output ('processing: ' + $Resource.Name) #set UpdateTags to true $UpdateTags = $false if ($Remove -or $Replace) { Write-Output ("Tag " + $ManageThisTag + " with value of " + $ManageThisTagValue + " exists in the " + $Resource.Name + " and will be Removed/Replaced") $Resource.Tag.Remove($ManageThisTag) if ($Replace) { $Resource.Tag.Add($NewTagKey, $NewTagValue) } #set UpdateTags to true $UpdateTags = $true } if ($Add) { $Resource.Tag.Add($NewTagKey, $NewTagValue) # In addition to the newtag and value, you can also hardcode any additonal tag value key pairs here # I know, it's not pretty, but it works if you want to add multiple tags #$Resource.Tags.Add("tag name","tag value") # Adding new tag and value #$Resource.Tags.Add("tag name","tag value") # Adding new tag and value #set UpdateTags to true $UpdateTags = $true } if ($UpdateTags) { Write-Output ("Updating Tags on: " + $Resource.Name) if (!$WhatIf) { $Resource | Set-AzureRmLoadBalancer } else { Write-Output "--- new tags ---" Write-Output ($Resource.Tag | Out-String) } } } } else { Write-Warning ("No resources found that matched search criteria") } |