2

i want to retrieve an ec2 instance's ID from it's name which I have set as the value of it's tag. I have a VM with key as 'Name' and value as 'testvm1'. Is there something like

(get-ec2 instance id) | where-object {$_.(key.value) - eq "testvm1"}

jeevanreddymandali
  • 395
  • 3
  • 8
  • 23

2 Answers2

2

this worked

 $ec2Name = New-Object Amazon.EC2.Model.Filter -Property @{Name = "tag:Name"; Values = "testvm1"}
    $instances = @(Get-EC2Tag -Filters $ec2Name) 
      $instances | Select-Object -ExpandProperty resourceid
jeevanreddymandali
  • 395
  • 3
  • 8
  • 23
2

You can further reduce this by passing a hashtable as your filter. This will map your input to a Amazon.EC2.Model.Filter[] type.

Example with one filter on tag:Name mapping multiple tag names:

(Get-EC2Tag -Filter @{Name="tag:Name";Values="Name1","Name2"}).ResourceId

Example output:

i-abcd1234
i-edfg5678
Anthony Neace
  • 25,013
  • 7
  • 114
  • 129