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
gefragt vor 2 Jahren324 Aufrufe
1 Antwort
1
Akzeptierte Antwort

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
beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen