powershellcommand-linecommand-promptcloud-foundry

Powershell how to print value of name in custom command


I have a command cf apps that outputs

This is the true output

name                                               requested state   processes   routes

accountdivisioncalculator__pte01__performance__0   started           web:2/2     accountdivisioncalculator-pte01.dev.schwab.tech, accountdivisioncalculator-pte01-e2e-ad00007734.scp2-test-app-bdv.dev.schwab.com
accountdivisioncalculator__qae01__e2e__0           started           web:1/1

How do I access all values of name, and then loop each app name

So the idea is to get the list of all app names and run the cf delete <app_name> so that I can avoid the hassle to delete the apps one by one.

This is the sample output after in run the command

enter image description here


Solution

  • Preface:


    Use the following, which makes the following assumptions, which are consistent with your sample data:

    # Initialize helper variables.
    $propNames = $null
    $ohtTemplate = [ordered] @{}
    
    # Parse the tabular CLI output into objects.
    $appInfos = 
      cf apps | 
        Where-Object { $_ } | # remove empty lines
        ForEach-Object {
          $fields = ($_ -split ' {2,}').Trim()
          if ($null -eq $propNames) { 
            $propNames = $fields
            foreach ($propName in $propNames) {
              $ohtTemplate[$propName] = $null
            }
            return 
          }
          foreach ($i in 0..($propNames.Count-1)) {
            if ($i -lt $fields.Count) {
              $ohtTemplate[$propNames[$i]] = $fields[$i]
            }
            else {
              $ohtTemplate[$propNames[$i]] = $null
            }
          }
          [pscustomobject] $ohtTemplate
        }
    
    # Output the resulting objects, for visual inspection.
    $appInfos
    

    The above emits custom objects whose property names correspond to the table header, and whose values contain a row's column values (which are all stored as strings).

    PowerShell's for-display output-formatting system conveniently renders the resulting objects as follows:

    name                                             requested state processes routes
    ----                                             --------------- --------- ------
    accountdivisioncalculator__pte01__performance__0 started         web:2/2   accountdivisioncalculator-pte01.dev.schwab.tech, accountdivisioncalculator-pte01-e2e-ad00007734.scp2-tes…
    accountdivisioncalculator__qae01__e2e__0         started         web:1/1   
    

    To get just the names, for instance, as an array, using member-access enumeration, use $appInfos.name:

    # Output from `$appInfos.name`
    accountdivisioncalculator__pte01__performance__0
    accountdivisioncalculator__qae01__e2e__0
    

    To get just the first app's name, use $appInfos[0].name.

    Of course, you can use $appInfos | ForEach-Object { <# work with $_ #> }, foreach ($app in $appInfos) { <# work with $app #> } or $appInfos.ForEach({ <# work with $_ #> }), as usual, to iterate over all app infos.