How to access the list value in the Get-EC2Instance RunningInstance method?

I am trying to get the instance name, public dns and the "Name" tag from the object returned by get-ec2instance .

 $instances = foreach($i in (get-ec2instance)) ' { $i.RunningInstance | Select-Object InstanceId, PublicDnsName, Tag } 

Here's the conclusion:

 InstanceId PublicDnsName Tag ---------- ------------- --- myInstanceIdHere myPublicDnsName {Name} ... ... {Name} 

I would like to access {Name} using the line of code above and print its value in this release. I did a little research with this initial post and found ...

 PS C:\Users\aneace\Documents> $instances[0].Tag.GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True List`1 System.Object 

Between this and the AWS docs, I think Tag refers to this list , but I'm not sure. I can access a table that prints columns of keys and values ​​by calling $instances[0].Tag , but now my problem is that I would like Value be the result for my first table instead of the {Name} object. Any suggestions?

+4
source share
1 answer

In the documentation, the Tag property is a list of Tag objects. Thus, in general, several keys / values ​​will be stored there. Do you assume that in your case there is only 1?

Select-Object allows you to capture not only raw property values, but also computed values. Say you just want a comma-separated list of Value objects from Tag objects in the list. Here's how you do it:

 $instances = Get-EC2Instance ` |%{ $_.RunningInstance } ` | Select-Object InstanceId,PublicDnsName,@{Name='TagValues'; Expression={($_.Tag |%{ $_.Value }) -join ','}} 

$instances TagValues elements will now have the TagValues property, which is a string consisting of Value from all the tags associated with the instance.

+5
source

Source: https://habr.com/ru/post/1495614/


All Articles