Public/Find-Record.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
86
87
88
89
90
91
92
93
94
95
function Find-Record {
    <#
        .SYNOPSIS
            Queries an AirTable table for one or more records.
    
        .EXAMPLE
            PS> Find-Record -BaseName foo -Table bar

            Returns all records in the bar table within the foo base.

        .EXAMPLE
            PS> Find-Record -BaseName foo -Table bar -FilterFormula '{Name}="adam"'

            Returns all records in the bar table within the foo base that have a Name value of 'adam'.

        .PARAMETER BaseName
            A string value representing the AirTable base that contains the table to query.

        .PARAMETER Table
            A string value representing the AirTable table containing the records to query.

        .PARAMETER FilterFormula
            A string value representing an AirTable-specific filter to limit the number of records returned. For
            full explanation of this query language, refer to
            https://support.airtable.com/hc/en-us/articles/203255215-Formula-field-reference.

        .PARAMETER View
            An optional string parameter representing a table view to limit results to.
    
    #>

    [OutputType('pscustomobject')]
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [Alias('BaseName', 'BaseId')]
        [string]$BaseIdentity,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$Table,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$FilterFormula,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$View,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [int]$MaxRecords = 100,

        [Parameter()]
        [ValidateSet('asc', 'desc')]
        [string]$SortDirection,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$SortField
        
    )

    $ErrorActionPreference = 'Stop'

    $baseId = GetBaseId -Identity $BaseIdentity

    $uri = BuildUriString -BaseId $baseId -Table $Table
    $invParams = @{
        Uri = $Uri
    }
    $httpBody = @{ }
    if ($PSBoundParameters.ContainsKey('FilterFormula')) {
        $httpBody['filterByFormula'] = $FilterFormula
    }
    if ($PSBoundParameters.ContainsKey('View')) {
        $httpBody['view'] = $View
    }
    if ($PSBoundParameters.ContainsKey('MaxRecords')) {
        $httpBody['maxRecords'] = $MaxRecords
    }
    if ($PSBoundParameters.ContainsKey('SortField')) {
        $httpBody['sort[0][field]'] = $SortField
        if ($PSBoundParameters.ContainsKey('SortDirection')) {
            $httpBody['sort[0][direction]'] = $SortDirection
        }
    }
    if ($httpBody.Keys -gt 0) {
        $invParams.HttpBody = $httpBody
    }
    InvokeAirTableApiCall @invParams
    
}