Public/Show-specTable.ps1
|
function Show-specTable { <# .SYNOPSIS Displays structured data in a formatted and colourised table format with a section header. .DESCRIPTION The Show-specTable function presents PowerShell data (such as hashtables or collections of objects) in a readable, formatted table layout within the console. It automatically adapts to display key-value pairs from hashtables or iterates through objects with properties to print each row clearly. A customisable section header is shown above the data for clarity and logical grouping. .PARAMETER Title Specifies the title text to display in the section header above the table. This parameter is mandatory. .PARAMETER Data The data to display in table format. This parameter accepts a hashtable, array, or collection of objects that can be enumerated. Each key-value pair or object property is written in aligned output. .PARAMETER HeaderColour Defines the colour of the section header text. Acceptable values are: Cyan, DarkCyan, Yellow, Red, Magenta, Green, and DarkGreen. The default value is Cyan. .PARAMETER Prefix Specifies the prefix label to display before the header title. Valid values are: PSE, INFO, and TASK. The default value is PSE. .EXAMPLE Show-specTable -Title "Device Information" -Data $deviceData Displays the contents of $deviceData in a structured table with a [PSE] Device Information header in cyan. .EXAMPLE Show-specTable -Title "Azure Table Results" -Data $results -HeaderColour Yellow -Prefix INFO Displays Azure table query results in a formatted table with a [INFO] Azure Table Results header in yellow. .NOTES Author: owen.heaume Version: 1.0 #> param( [Parameter(Mandatory)] [string]$Title, [Parameter(Mandatory)] [object]$Data, [ValidateSet('Cyan', 'DarkCyan', 'Yellow', 'Red', 'Magenta', 'Green', 'DarkGreen')] [string]$HeaderColour = 'Cyan', [ValidateSet('PSE', 'INFO', 'TASK')] [string]$Prefix = 'PSE' ) Show-specSectionHeader -Title $Title -Colour $HeaderColour -Prefix $Prefix if ($Data -is [System.Collections.Hashtable]) { $Data.GetEnumerator() | ForEach-Object { Write-Host ('{0,-40}: {1}' -f $_.Key, $_.Value) -ForegroundColor DarkGray } } elseif ($Data -is [System.Collections.IEnumerable]) { foreach ($row in $Data) { foreach ($key in $row.Keys) { Write-Host ('{0,-40}: {1}' -f $key, $row[$key]) -ForegroundColor DarkGray } # Only add a blank line if row has multiple columns if ($row.Keys.Count -gt 1) { Write-Host '' } } } } |