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'.
    #>


    [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) {
                switch ($item) {
                    "Country" {
                        $finalData += $row | Where-Object {
                            $_.Country -eq $Country -and
                            $_.Device -eq 'ALL' -and
                            $_.epos -eq 'ALL' -and
                            $_.Persona -eq 'ALL'
                        }
                    }
                    "Device" {
                        $finalData += $row | Where-Object {
                            $_.Device -eq $Device -and
                            $_.country -eq 'ALL' -and
                            $_.epos -eq 'ALL' -and
                            $_.persona -eq 'ALL'
                        }
                    }
                    "EPOS" {
                        $finalData += $row | Where-Object {
                            $_.EPOS -eq $epos -and
                            $_.country -eq 'ALL' -and
                            $_.Device -eq 'ALL' -and
                            $_.Persona -eq 'ALL'
                        }
                    }
                    "Persona" {
                        $finalData += $row | Where-Object {
                            $_.Persona -eq $Persona -and
                            $_.country -eq 'ALL' -and
                            $_.Device -eq 'ALL' -and
                            $_.epos -eq 'ALL'
                        }
                        $finalData += $row | Where-Object { # Persona AND Epos
                            $_.Persona -eq $Persona -and
                            $_.epos -eq $epos -and
                            $_.country -eq 'ALL' -and
                            $_.Device -eq 'ALL'
                        }
                    }
                }
            }
        }
    }

    end {
        $finalData
    }
}