Wednesday, October 12, 2016

Powershell Script to add Notification for all ASGs - AWS Cloud

<#  
.SYNOPSIS  
Script to add Notification for all ASGs

FILE NAME

AddNotifASG.ps1
#>

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$CommonFilePath = $scriptDir + "\AWSCredentials.ps1"
. $CommonFilePath


$Environment = "PROD"

$AutoScalingGroupName=""

try
{
   $AutoScalingGroups = (Get-ASAutoScalingGroup | Where-Object -FilterScript {
                        $_.Tags | Where-Object {
                                ($_.Key -eq "Environment" -and $_.Value -eq $Environment)
                                }             
                            }) 

    foreach($AutoScalingGroup in $AutoScalingGroups)
    {
        $AutoScalingGroupName = $AutoScalingGroup.AutoScalingGroupName
        Write-ASNotificationConfiguration -AutoScalingGroupName $AutoScalingGroupName -NotificationType @("autoscaling:EC2_INSTANCE_LAUNCH", "autoscaling:EC2_INSTANCE_TERMINATE") -TopicARN "{Specify the ARN Value here}"
    }

}

catch 
{
$originalException = $_.Exception 
try
{
"Error: " + $originalException.Message
}
catch

"Original Error: " + $originalException.Message
"Logging Error:" + $_.Exception.Message
}

    Exit 1
}

No comments:

Post a Comment