private/get/Get-Instance.ps1
function Get-Instance { [CmdletBinding(PositionalBinding = $true)] [OutputType([System.Collections.ArrayList])] param() $results = [System.Collections.ArrayList]::new() $ec2Instances = Get-EC2Instance | Select-Object -ExpandProperty 'Instances' foreach ($instance in $ec2Instances) { $launchTimespan = New-TimeSpan -Start $instance.LaunchTime -End (Get-Date) if ($launchTimespan.Days -gt 0) { $launchTimeMessage = "$($launchTimespan.Days.ToString().PadLeft(4)) Days" } elseif ($launchTimespan.Hours -gt 0) { $launchTimeMessage = "$($launchTimespan.Hours.ToString().PadLeft(4)) Hours" } else { $launchTimeMessage = "$($launchTimespan.Minutes.ToString().PadLeft(4)) Minutes" } $result = [pscustomobject]@{ InstanceId = $instance.InstanceId KeyName = $instance.KeyName PlatformType = $instance.PlatformDetails InstanceName = $instance.Tags | Where-Object -Property 'Key' -eq -Value 'Name' | Select-Object -ExpandProperty 'Value' Status = $instance.State.Name AvailabilityZone = $instance.Placement.AvailabilityZone LaunchTimeMessage = $launchTimeMessage } $results.Add($result) | Out-Null } if (-not $results) { Write-Host '' Write-Host 'No instances found in this region.' -ForegroundColor DarkYellow } return $results } |