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) { 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 (![string]::IsNullOrEmpty($row.all)) { $finalData += $row; break } switch ($item) { 'Country' { $finalData += $row | Where-Object { $_.Country -eq $Country -and [string]::IsNullOrEmpty($_.Device) -and [string]::IsNullOrEmpty($_.epos) -and [string]::IsNullOrEmpty($_.Persona) -and ![string]::IsNullOrEmpty($Country) } } 'Device' { $finalData += $row | Where-Object { $_.Device -eq $Device -and [string]::IsNullOrEmpty($_.country) -and [string]::IsNullOrEmpty($_.epos) -and [string]::IsNullOrEmpty($_.persona) -and ![string]::IsNullOrEmpty($Device) } } 'EPOS' { $finalData += $row | Where-Object { $_.EPOS -eq $epos -and [string]::IsNullOrEmpty($_.country) -and [string]::IsNullOrEmpty($_.Device) -and [string]::IsNullOrEmpty($_.Persona) -and ![string]::IsNullOrEmpty($epos) } } 'Persona' { $finalData += $row | Where-Object { $_.Persona -eq $Persona -and [string]::IsNullOrEmpty($_.country) -and [string]::IsNullOrEmpty($_.Device) -and [string]::IsNullOrEmpty($_.epos) -and ![string]::IsNullOrEmpty($Persona) } $finalData += $row | Where-Object { # Persona AND Epos $_.Persona -eq $Persona -and $_.epos -eq $epos -and [string]::IsNullOrEmpty($_.country) -and [string]::IsNullOrEmpty($_.Device) -and ![string]::IsNullOrEmpty($Persona) -and ![string]::IsNullOrEmpty($epos) } } } } } } end { $finalData } } |