根据特定标签列出或过滤 EC2 实例

根据特定标签列出或过滤 EC2 实例

有人知道如何使用 PowerShell 根据特定标签过滤 AWS EC2 实例吗?

我用过了:

$instance = Get-EC2Instance -Filter @( @{name='tag:<name>'; values="<IIS>"}; @{name='instance-state-code'; values = 16} ) | Select-Object -ExpandProperty instances

还尝试过:

"require 'aws-sdk-ec2'  
ec2 = Aws::EC2::Resource.new(region: 'us-east-1') ec2.instances({filters: [{name: 'tag:<name>', values: ['<IIS>']}]})"

答案1

你走对了路,看看这个关联

$searchFor =@(
    @{
        name = 'tag:Environment'
        values = "Dev"
    }
)
Get-EC2Instance -Filter $searchFor

高级搜索功能,可在网站上找到:

Function ConvertTo-EC2Filter
{
    [CmdletBinding()]
    Param(
        [Parameter(
            ValueFromPipeline,
            ValueFromPipelineByPropertyName)]
        [HashTable]
        $Filter
    )
    Begin
    {
        $ec2Filter = @()    
    }
    Process
    {

        $ec2Filter = Foreach ($key in $Filter.Keys)
        {
            @{
                name   = $key
                values = $Filter[$key]
            }
        }
    }
    End
    {
        $ec2Filter
    }
}

相关内容