Reputation: 19
$var=(Get-EC2Instance -Credential $Creds).Instances | select InstanceID, @{Name="Servername";Expression={$_.tags | where key -eq "Name" | select Value -expand Value}}
Now $var holds the value like below
InstanceID | Servername
--------- |----------
Inst1 | A
Inst2 | B
Inst3 | C
How do I return InstanceID based on server name
Upvotes: 0
Views: 704
Reputation: 9601
This is similar to Lee_Daily's comment above, but it should also work.
$var = (Get-EC2Instance -Credential $Creds).Instances | select InstanceID, @{ Name="Servername"; Expression = {$_.tags | where key -eq "Name" | select Value -expand Value} }
function GetInstanceId($serverData, $serverName) {
$var | Where-Object {
$_.ServerName -eq $serverName
}
}
$instanceData = GetInstanceId -serverData $var -serverName 'A'
Write-Host $instanceData.InstanceID
Upvotes: 1