Public/Get-specFilteredData.ps1
Function Get-specFilteredData { <# .SYNOPSIS Filters table data based on specific criteria. .DESCRIPTION This function filters an array of table data based on the specified country, device, EPOS, or persona. It supports filtering by individual criteria or by combinations of persona and EPOS. The function returns an array of filtered data rows. .PARAMETER tableData An array of hashtables containing the table data to be filtered. Each hashtable represents a row in the table and should have properties corresponding to 'Country', 'Device', 'EPOS', and 'Persona'. .PARAMETER Country The country to filter by. If specified, only rows with the matching country value will be included. Use 'ALL' to match all countries. .PARAMETER Device The device to filter by. If specified, only rows with the matching device value will be included. Use 'ALL' to match all devices. .PARAMETER EPOS The EPOS to filter by. If specified, only rows with the matching EPOS value will be included. Use 'ALL' to match all EPOS. .PARAMETER Persona The persona to filter by. If specified, only rows with the matching persona value will be included. Use 'ALL' to match all personas. .EXAMPLE $filteredData = Get-specFilteredData -tableData $myTableData -Country 'USA' -Device 'ALL' -EPOS 'ALL' -Persona 'ALL' Filters $myTableData to return only rows with the country 'USA'. .EXAMPLE $filteredData = Get-specFilteredData -tableData $myTableData -Country 'ALL' -Device 'Mobile' -EPOS 'ALL' -Persona 'ALL' Filters $myTableData to return only rows with the device 'Mobile'. .EXAMPLE $filteredData = Get-specFilteredData -tableData $myTableData -Country 'ALL' -Device 'ALL' -EPOS 'SystemX' -Persona 'ALL' Filters $myTableData to return only rows with the EPOS 'SystemX'. .EXAMPLE $filteredData = Get-specFilteredData -tableData $myTableData -Country 'ALL' -Device 'ALL' -EPOS 'ALL' -Persona 'Manager' Filters $myTableData to return only rows with the persona 'Manager'. .EXAMPLE $filteredData = Get-specFilteredData -tableData $myTableData -Country 'ALL' -Device 'ALL' -EPOS 'SystemX' -Persona 'Manager' Filters $myTableData to return only rows with the persona 'Manager' and EPOS 'SystemX'. .NOTES Author: owen.heaume Version 1.0 #> [cmdletbinding()] param( [array]$tableData, [string]$Country, [string]$Device, [string]$EPOS, [string]$Persona ) Begin { $finalData = @() $tableFilters = @('Country', 'Device', 'Epos', 'Persona') } process { foreach ($row in $tableData) { foreach ($item in $tableFilters) { # If device name is present then this trumps all other filters and will be included if ($device -eq $row.device) { Write-Host 'Device name found - ignoring other filters' -ForegroundColor DarkYellow $finalData += $row break } # If there is an exclusion match then the row should not be added if ($row.exclude) { if ($EPOS -eq $row.exclude) { Write-Host "Exclusion found: $($row.exclude)" -ForegroundColor DarkYellow; break } if ($device -eq $row.exclude) { Write-Host "Exclusion found: $($row.exclude)" -ForegroundColor DarkYellow; break } if ($country -eq $row.exclude) { Write-Host "Exclusion found: $($row.exclude)" -ForegroundColor DarkYellow; break } if ($Persona -eq $row.exclude) { Write-Host "Exclusion found: $($row.exclude)" -ForegroundColor DarkYellow; break } } #If the 'All' header in the table is populated then add the row if (![string]::IsNullOrEmpty($row.all)) { $finalData += $row; break } # If all four filters are set to ALL then the row should be added if ($row.country -eq 'ALL' -and $row.persona -eq 'ALL' -and $row.EPOS -eq 'ALL' -and $row.Country -eq 'ALL') { Write-Host 'All filters set to all - adding row' -ForegroundColor DarkYellow $finalData += $row break } switch ($item) { 'Country' { $finalData += $row | Where-Object { $_.Country -eq $Country -and $_.Device -eq 'ALL' -and $_.epos -eq 'ALL' -and $_.Persona -eq 'ALL' -and ![string]::IsNullOrEmpty($_.country) } } 'Device' { $finalData += $row | Where-Object { $_.Device -eq $Device -and $_.country -eq 'ALL' -and $_.epos -eq 'ALL' -and $_.persona -eq 'ALL' -and ![string]::IsNullOrEmpty($_.device) } } 'EPOS' { $finalData += $row | Where-Object { $_.EPOS -eq $epos -and $_.country -eq 'ALL' -and $_.Device -eq 'ALL' -and $_.Persona -eq 'ALL' -and ![string]::IsNullOrEmpty($_.epos) } } 'Persona' { $finalData += $row | Where-Object { $_.Persona -eq $Persona -and $_.country -eq 'ALL' -and $_.Device -eq 'ALL' -and $_.epos -eq 'ALL' -and ![string]::IsNullOrEmpty($_.persona) } $finalData += $row | Where-Object { # Persona AND Epos $_.Persona -eq $Persona -and $_.epos -eq $epos -and $_.country -eq 'ALL' -and $_.Device -eq 'ALL' -and ![string]::IsNullOrEmpty($Persona) -and ![string]::IsNullOrEmpty($_.epos) } } } } } } end { $finalData } } |