Private/ConvertTo-FilterString.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Function ConvertTo-FilterString {
    <#
.SYNOPSIS
Converts Hashtable Key-Value pairs to a string for use as a Filter value

.DESCRIPTION
When given a hashtable as input, converts key value pairs to filter string.
Returns single key hashtable, with string as value of the "filter" key.
If an input key is `modificationTime` (from Get-PASAccount), it is expected to have a datetime value;
this will be converted to unixtime, the operator for the filter value will be 'gte' rather than 'eq'.

.PARAMETER Parameters
Hashtable containing parameter names and values to include in output

.EXAMPLE
$input | ConvertTo-FilterString

Joins Key & Value with "eq"
Joins Multiple Key Value pairs with ' AND '
Output: @{"filter" = "Key eq Value AND Key eq Value"}

.EXAMPLE
@{modificationTime=(Get-Date)} | ConvertTo-FilterString

@{"filter" = "modificationTime eq 12345678"}


Returns datetime as unixtime, with `gte` operator.

#>

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'FilterList', Justification = "False Positive")]
    [CmdletBinding()]
    [OutputType('System.Hashtable')]
    param(
        [parameter(
            Mandatory = $false,
            ValueFromPipeline = $true
        )]
        [hashtable]$Parameters
    )

    Begin {

    }

    Process {

        If ($Parameters) {

            $Parameters.Keys | ForEach-Object {

                $FilterList = [Collections.Generic.List[Object]]@()

            } {
                switch ($PSItem) {

                    modificationTime {

                        $null = $FilterList.Add("modificationTime gte $($Parameters[$PSItem] | ConvertTo-UnixTime)")

                    }

                    default {

                        $null = $FilterList.Add("$PSItem eq $($Parameters[$PSItem])")

                    }

                }
            } {

                If ($FilterList.count -gt 0) {

                    @{"filter" = $FilterList -join " AND " }

                }
            }

        }

    }

    End { }

}