public/Compare-StringArraySubset.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function Compare-StringArraySubset {
    [CmdletBinding()]
    param (
        # The full list of valid values for an input array
        [Parameter(Mandatory,Position=0)]
        [ValidateNotNullOrEmpty()]
        [string[]]
        $Superset,

        # The list of values to be validated against the superset
        [Parameter(Mandatory,Position=1)]
        [ValidateNotNullOrEmpty()]
        [string[]]
        $Subset,

        # Set this flag to treat items in the superset as regular expressions
        [Parameter()]
        [switch]
        $Regex
    )
    process {
        $toReturn = $true
        $Subset | ForEach-Object { 
            $eval = $false
            if ($Regex) {
                $item = $_
                $Superset | ForEach-Object { if ($item -match $_) { $eval = $true } }
            } else {
                $eval = $Superset -contains $_
            }
            if (!$eval) {$toReturn = $false} 
        }
        $toReturn
    }
}