ManageTagsOnLoadBalancer.ps1


<#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")
}