使用PowerShell创建可移植脚本以列出EIP。

0

【以下的问题经过翻译处理】 创建一个脚本,在给定帐户中列出eu-west-1和us-west-2地区的EC2弹性IP地址,并为它们分配一个标签“TBA”。输出应该是标签更新和公共IP的确认。 我在创建脚本过程中遇到了困难:

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

我尝试了不同的来源,但仍然无法获得正确的命令,请帮忙。

profile picture
专家
已提问 5 个月前8 查看次数
1 回答
0

【以下的回答经过翻译处理】 您提供的示例缺少一个命令New-EC2Tag以将标记添加到弹性IP分配。此外,您没有使用正确的过滤器来获取带有标记(Name:TBA)的IP。请参见下面的示例脚本。您可以根据需要进行自定义。

#设置地区
$regions = @(“eu-west-1”,“us-west-2”)

#AWS PowerShell文档https://docs.aws.amazon.com/powershell/latest/reference/
foreach($region in $regions){
    #https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Address.html
    #提取弹性IP分配ID
    $allocationIds = Get-EC2Address -Region $region | ForEach-Object { $_.AllocationId }

    #准备要添加的标记
    $tag = New-Object Amazon.EC2.Model.Tag 
    $tag.Key =“Name” 
    $tag.Value =“TBA”

    #标记所有弹性IPs
    try {
        #https://docs.aws.amazon.com/powershell/latest/reference/items/New-EC2Tag.html
        #添加新标记
        New-EC2Tag -Resource $allocationIds -Tag $tag
    } catch {
        #添加标记失败
        Write-Error $_.ErrorDetails
        throw
    }
    #如果标记成功,则再次获取IP
    Get-EC2Address -Filter @{Name =“tag: Name”; Values =“TBA”}
}
profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则