Public/Get-specHUNSCFilteredData.ps1

Function Get-specSCFilteredData {
    <#
.SYNOPSIS
Filters table data based on specified DPGGroup and Location criteria for Supply Chain PSEngine deployments.
 
.DESCRIPTION
The Get-specHUNSCFilteredData function filters an input array of table data by evaluating combinations of 'DPGGroup' and 'Location' values.
It returns rows that match any of the following:
- DPGGroup equals the specified value and Location is 'ALL'
- Both DPGGroup and Location match the specified values
- DPGGroup and Location are both 'ALL'
- DPGGroup is 'ALL' and Location matches the specified value
 
This is useful for hierarchical or fallback matching logic in scenarios where 'ALL' serves as a wildcard or global scope.
 
.PARAMETER tableData
An array of objects representing the data table to filter. Each object must contain the properties 'DPGGroup' and 'Location'.
 
.PARAMETER DPGGroup
The DPG group value to filter the table data by.
 
.PARAMETER Location
The location value to filter the table data by.
 
.OUTPUTS
Array
Returns an array of filtered objects that match the specified criteria.
 
.EXAMPLE
PS C:\> Get-specSCFilteredData -tableData $data -DPGGroup 'DPG001' -Location 'LON'
 
Returns all rows from $data that match DPGGroup 'DPG001' and/or Location 'LON', according to the defined rules.
 
.NOTES
    Author - owen.heaume
    Version - 1.0 Initial Release
    Date - 1.1 Rename function
#>


    [cmdletbinding()]

    param(
        [array]$tableData,

        [string]$DPGGroup,
        [string]$Location
    )

    begin {
        $finalData = @()
        $tableFilters = @('DPGGroup', 'Location')
    }

    process {
        foreach ($row in $tableData) {
            foreach ($filter in $tableFilters) {
                switch ($filter) {
                    'DPGGroup' {
                        $finalData += $row | ? {
                            $_.DPGGroup -eq $DPGGroup -and
                            $_.Location -eq 'ALL' -and
                            ![string]::IsNullOrEmpty($_.DPGGroup)
                        }

                        $finalData += $row | ? {
                            $_.DPGGroup -eq $DPGGroup -and #DPGGroup and Location
                            $_.Location -eq $Location -and
                            ![string]::IsNullOrEmpty($_.DPGGroup) -and
                            ![string]::IsNullOrEmpty($_.Location)
                        }

                        $finalData += $row | ? {
                            $_.DPGGroup -eq 'ALL' -and
                            $_.Location -eq 'ALL' -and
                            ![string]::IsNullOrEmpty($_.DPGGroup) -and
                            ![string]::IsNullOrEmpty($_.Location)
                        }
                    }
                    'Location' {
                        $finalData += $row | ? {
                            $_.Location -eq $location -and
                            $_.DPGGroup -eq 'ALL' -and
                            ![string]::IsNullOrEmpty($_.Location)
                        }
                    }
                }
            }
        }
    }

    end {
        $finalData
    }
}