ps/Modules/Cole.PowerShell.Developer/Public/ConvertFrom-EC2Instance.ps1
2023-05-30 22:51:22 -07:00

90 lines
3.5 KiB
PowerShell

function ConvertFrom-EC2Instance {
<#
.SYNOPSIS
Convert the results of Get-EC2Instance to a smaller data size
.PARAMETER Instances
One or more EC2Instance results
#>
[CmdletBinding()]
param (
[Parameter()]
[object[]]$Instances
)
$activityLabel = "Gathering instance information"
if ($null -eq $Instances) {
return
}
if ($null -ne $Instances.Instances) {
# The Get-EC2Instance call returns a nested object called Instances
# We only want that information tho
$Instances = $Instances.Instances
}
$allInstances = @()
$instancesProcessed = 0
foreach ($instance in $Instances) {
$innerPercentComplete = [Math]::Floor(100 * $instancesProcessed / $Instances.Count)
Write-ProgressHelper -Activity $activityLabel -Status $instance.InstanceId -PercentComplete $innerPercentComplete
$tags = @{}
foreach ($tag in $instance.Tags) {
$tags[$tag.Key] = $tag.Value
}
$designationTag = 'designation'
foreach($tagname in @('box','lane','pod','designation')) {
$designation = $tags["alk:$tagname"]
if (![string]::IsNullOrWhiteSpace($designation)) {
$designationTag = $tagname
break
}
}
$networkInterfaces = @()
foreach ($networkInterface in $instance.NetworkInterfaces) {
$networkInterfaces += @{
Groups = $networkInterface.Groups
Ipv6Addresses = $networkInterface.Ipv6Addresses
MacAddress = $networkInterface.MacAddress
PrivateDnsName = $networkInterface.PrivateDnsName
PrivateIpAddress = $networkInterface.PrivateIpAddress
PrivateIpAddresses = $networkInterface.PrivateIpAddresses
SubnetId = $networkInterface.SubnetId
VpcId = $networkInterface.VpcId
}
}
$hostname = $instance.Tags.Where({$_.Key -eq "alk:hostname"}).Value
if (![string]::IsNullOrWhiteSpace($hostname)) {
$hostname = "$hostname.fh.local"
}
$allInstances += @{
AvailabilityZone = $instance.Placement.AvailabilityZone
CaptureState = $instance.State.Name.Value
CpuOptions = $instance.CpuOptions
EnaSupport = $instance.EnaSupport
IamInstanceProfile = $instance.IamInstanceProfile.Arn
ImageId = $instance.ImageId
InstanceId = $instance.InstanceId
InstanceType = $instance.InstanceType.Value
NetworkInterfaces = $networkInterfaces
PrivateDnsName = $instance.PrivateDnsName
PrivateIpAddress = $instance.PrivateIpAddress
PublicDnsName = $instance.PublicDnsName
PublicIpAddress = $instance.PublicIpAddress
Region = $region
SecurityGroups = $instance.SecurityGroups | ForEach-Object { @{ GroupId = $_.GroupId; GroupName = $_.GroupName; }}
SubnetId = $instance.SubnetId
VpcId = $instance.VpcId
Tags = $tags
Hostname = $hostname
Designation = $designation
DesignationTag = $designationTag
Role = $instance.Tags.Where({$_.Key -eq "alk:role"}).Value
Service = $instance.Tags.Where({$_.Key -eq "alk:service"}).Value
}
$instancesProcessed += 1
}
Write-Progress -Activity $activityLabel -Completed
return ($allInstances | ConvertTo-Instance)
}