0

I am attempting to enumerate values from a hashtable.

Typically this is done by using the GetEnumerator() method or using a foreach statement like here.

However, I am building custom tables to maintain maximum flexibility down the pipeline.

# Create Custom Table
$ExpandedTable =
    @{e={$_.person.height};label='Height'},
    @{e={$_.person.weight};label='Weight'},
    @{e={$_.person.age};label='Age'},
    @{e={$_.person.Certifications};label='Certifications}'}

# Cmdlet
function Get-Person {
    param(
        [Parameter(Mandatory=$true,HelpMessage='Please enter a Name')]
        [string]$person
        [switch]$Expanded
    )

    if($person -match $Regex) {
        $PersonelProperties = Invoke-RestMethod -Method Get -Uri https://www.url.com/api/person -Body @{ apikey = $mykey
          secret = $somesecret 
    }
    if ($PersonelProperties.response_code -eq '0' -and $PersonelProperties.response.Count -eq 0) {
        Write-Warning -Message 'Person not found in Data Base!'
    } elseif ($PersonelProperties.response_code -eq '0' -and $PersonelProperties.response.Count -gt 0) {
        if ($Expanded -eq $true) { 
            # Attempting to create table from Data called from API here
            Write-Verbose -Message 'Creating Table'
            $PersonelProperties | Select-Object -Property $ExpandedTable
        } else {
            Write-Verbose -Message 'Creating Table'
            $PersonelProperties | Select-Object -Property $RegularTable
        }
    }
}

Goal:

I want the following output:

PS > Get-Person 'John Doe'

Height                  : 73
Weight                  : 230
Age                     : 28
Certifications          : {GCIA, GFCE, OSCP, GREM}

I want to remove the Certification values from the hashtable array {GCIA, GFCE, OSCP, GREM}

To look like this:

Height                  : 73
Weight                  : 230
Age                     : 28
Certifications          : GCIA, GFCE, OSCP, GREM

If there is a better practice to accomplish this, any suggestions are taken seriously.

Community
  • 1
  • 1
Alexander Sinno
  • 554
  • 5
  • 24

1 Answers1

2

Join the Certifications list in your calculated property:

@{e={$_.person.Certifications -join ', '};label='Certifications}'}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328