Create portable script in Powershell to list EIPs

0

Create a script List EC2 Elastic IP addresses for eu-west-1 and us-west-2 regions for a given account and assign a tag 'TBA' to all of them. The output should be a confirmation of Tag update and Public IP. I'm having difficulty in the script: Get-EC2Address -AccountId 123456789 $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "Name" $tag.Value = "TBA" Get-EC2Address -Filter @{Name="tag:Category";Values="Prod"}

I was trying different sources, but still cant get proper commands please help

AWS
preguntada hace 2 años316 visualizaciones
1 Respuesta
1
Respuesta aceptada

The example you provided is lacking a command New-EC2Tag to add a tag to an elastic IP allocation. Additionally, you are not using the right filter to get IPs with a tag (Name:TBA). Please see the below for a sample script. You can customize it as needed.

# set regions
$regions = @("eu-west-1", "us-west-2")

# AWS PowerShell documentation https://docs.aws.amazon.com/powershell/latest/reference/
foreach ($region in $regions) {
    # https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Address.html
    # Extracting elastic IP allocation IDs
    $allocationIds = Get-EC2Address -Region $region | ForEach-Object { $_.AllocationId }

    # Prepare a Tag to Add
    $tag = New-Object Amazon.EC2.Model.Tag 
    $tag.Key = "Name" 
    $tag.Value = "TBA"
    
    # Tag all the elasticIPs
    try {
        # https://docs.aws.amazon.com/powershell/latest/reference/items/New-EC2Tag.html
        # add a new tag
        New-EC2Tag -Resource $allocationIds -Tag $tag
    } catch {
        # Failed to add a tag
        Write-Error $_.ErrorDetails
        throw
    }
    # if tagging is successful, get IPs again
    Get-EC2Address -Filter @{Name="tag:Name";Values="TBA"}
}

AWS
Taka_M
respondido hace 2 años

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas