Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format-Table not displaying in function

Tags:

powershell

I have a section of PowerShell code that reads a list of items from Azure, and formats them into a table for the user to choose from:

if ($SubscriptionArray.Count -eq 1) {
    $SelectedSub = 1
}
# Get SubscriptionID if one isn't provided
while ($SelectedSub -gt $SubscriptionArray.Count -or $SelectedSub -lt 1) {
    Write-host "Please select a subscription from the list below"
    $SubscriptionArray | Select-Object "#", Id, Name | Format-Table
    try {
        $SelectedSub = Read-Host "Please enter a selection from 1 to $($SubscriptionArray.count)"
    }
    catch {
        Write-Warning -Message 'Invalid option, please try again.'
    }
}

When executed in the main area of the script, this outputs the expected result:

AzureTableOutput

I want to use this logic multiple times, and therefore moved it into a method:

function Get-IndexNumberFromArray(
    [Parameter(Mandatory = $True)]
    [array]$selectArray,

    [Parameter(Mandatory = $True)]
    [string]$message
) {
    [int]$SelectedIndex = 0
    # use the current subscription if there is only one subscription available
    if ($selectArray.Count -eq 1) {
        $SelectedIndex = 1
    }

    # Get SubscriptionID if one isn't provided
    while ($SelectedIndex -gt $selectArray.Count -or $SelectedIndex -lt 1) {
        Write-Host "$message"

        $selectArray | Select-Object "#", Id, Name | Format-Table

        try {
            $SelectedIndex = Read-Host "Please enter a selection from 1 to $($selectArray.count)"
        }
        catch {
            Write-Warning -Message 'Invalid option, please try again.'
        }
    }

    return $SelectedIndex
}

Everything in this method works great, except now my table is no longer outputting to the window. Instead, the user just get a prompt to pick a number from 1 to x with no context for what each number represents.

Why is the table working in the main area of the script, but not working in a function?

like image 366
Bic Avatar asked Mar 12 '18 14:03

Bic


People also ask

What is the function of Format-Table?

The Format-Table cmdlet formats the output of a command as a table with the selected properties of the object in each column. The object type determines the default layout and properties that are displayed in each column. You can use the Property parameter to select the properties that you want to display.

How do you display data in a Table Format in PowerShell?

Using Format-Table for Tabular Output. If you use the Format-Table cmdlet with no property names specified to format the output of the Get-Process command, you get exactly the same output as you do without a Format cmdlet. By default, PowerShell displays Process objects in a tabular format.

What function does the Format-list FL switch perform?

Format-List, or FL for short, allows PowerShell to control the output of your main script. Whenever presentation of information is important, pipe the script's output into Format-List (or Format-Table).

What is the function of Format wide cmdlet?

Description. The Format-Wide cmdlet formats objects as a wide table that displays only one property of each object. You can use the Property parameter to determine which property is displayed.


1 Answers

Format-Table actually doesn't print a table, it outputs objects that are then printed as the table. So if you're using a function, then the Format-Table output gets part of the return value of your function.

You can add Out-Host to the pipeline to force Format-Table's result to end up on the host, i.e. the console:

$selectArray | Select-Object "#", Id, Name | Format-Table | Out-Host
like image 184
Joey Avatar answered Sep 28 '22 11:09

Joey