function Get-AWSCredentialEntries { <# .SYNOPSIS Used to read/parse the AWS credential entries to a queryable format #> [CmdletBinding()] [OutputType([object[]])] param ( ) $logLead = (Get-LogLeadName) $awsCredentialPath = (Expand-Path "~/.aws/credentials") if (!(Test-Path $awsCredentialPath)) { 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 $awsCredentialPath) $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.Profile) { # $profile object is populated, save it $objects += $profile } $name = $line.Replace(']','').Replace('[','') $profile = @{ Profile = $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.Profile) { $objects += $profile } return ($objects | ConvertTo-AWSCredentialEntry | Sort-Object) }