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

52 lines
1.6 KiB
PowerShell

function Get-AWSConfigEntries {
<#
.SYNOPSIS
Used to read/parse the AWS config entries to a queryable format
#>
[CmdletBinding()]
[OutputType([object[]])]
param (
)
$logLead = (Get-LogLeadName)
$awsConfigPath = (Expand-Path "~/.aws/config")
if (!(Test-Path $awsConfigPath)) {
Write-Error "$logLead : Can not get path, are you sure you've setup your profile for AWS? Maybe run {function:Initialize-AWSCredentials}"
return
}
$objects = @()
$lines = (Get-Content $awsConfigPath)
$profile = @{}
foreach ($line in $lines) {
if ($line.StartsWith('#')) {
# this is a comment, ignore it
} elseif ($line.StartsWith('[')) {
# this is a new profile, push any old profile into the objects array and start fresh
if ($null -ne $profile.Name) {
# $profile object is populated, save it
$objects += $profile
}
$name = $line.Replace(']','').Split(' ')[1]
$profile = @{ Name = $name }
} elseif ([string]::IsNullOrWhiteSpace($line)) {
# ignore empty lines, natch
} else {
# the only thing left are valid properties to attach to an object
$splits = $line -split '='
$name = $splits[0].Trim()
$value = $splits[1].Trim()
$profile.$name = $value
}
}
# save the last value if it's populated (which it should be)
if ($null -ne $profile.Name) {
$objects += $profile
}
return ($objects | ConvertTo-AWSConfigEntry | Sort-Object)
}