Wednesday, October 12, 2016

Powershell Script to Remove Tags for ASG and associated instances - AWS Cloud

<#  
.SYNOPSIS  
Script to Remove Tags for ASG and associated instances by reading the ASG's from file

.FILE NAME
RemoveTags.ps1
#>

clear

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

$Asg_FilePath = $scriptDir + "\ASG.txt"

$AutoScalingGroups = Get-Content $Asg_FilePath

foreach($AutoScalingGroup in $AutoScalingGroups)
{
    $Tag = New-Object Amazon.AutoScaling.Model.Tag
    $Tag.ResourceId = $AutoScalingGroup
    $Tag.ResourceType = "auto-scaling-group"
    $Tag.Key = "{Specify the Tag Key}"
    Remove-ASTag -Tag $Tag -Force
    "Removed Tag for ASG " + $AutoScalingGroup

    $Instances = (Get-ASAutoScalingGroup -AutoScalingGroupName $AutoScalingGroup).Instances
    foreach ($Instance in $Instances)
    {
        $tag = New-Object Amazon.EC2.Model.Tag
        $tag.Key = "{Specify the Tag Key}"
        Remove-EC2Tag -Resource $Instance.InstanceId -tag $tag -Force
        "Removed Tag for Instance " + $Instance.InstanceId
    }
}

No comments:

Post a Comment