Public/Update-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
96
97
98
99
100
101
102
103
function Update-Record {
    <#
        .SYNOPSIS
            Updated an AirTable table record field value(s).
    
        .EXAMPLE
            PS> Find-Record -BaseName foo -Table bar -Name 'old' | Update-Record -Fields { 'Name' = 'new' }

            Updates any records in the 'bar' table with a field Name value of 'old' to 'new'.

        .EXAMPLE
            PS> Update-Record -BaseName foo -Table bar -Id recXXXXXXXXX -Fields { 'Name' = 'new' }

            Updates any records in the 'bar' table with a field Name value of 'old' to 'new'.
        
        .PARAMETER InputObject
            A pscustomobject value representing the record to update. This is typically used via the pipeline.

        .PARAMETER Id
            A string value representing the record ID of the record to update.

        .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 Fields
            A hashtable value representing all of the record's fields to update. Each key in the hashtable is the
            field name and each corresponding value is the value to update the field to.
    
    #>

    [OutputType('void')]
    [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'Default')]
    param
    (
        [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'ByObject')]
        [ValidateNotNullOrEmpty()]
        [pscustomobject]$InputObject,

        [Parameter(Mandatory, ParameterSetName = 'ById')]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern('^rec')]
        [string]$Id,

        [Parameter(Mandatory, ParameterSetName = 'ById')]
        [ValidateNotNullOrEmpty()]
        [Alias('BaseName', 'BaseId')]
        [string]$BaseIdentity,

        [Parameter(Mandatory, ParameterSetName = 'ById')]
        [ValidateNotNullOrEmpty()]
        [string]$Table,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [hashtable]$Fields,

        [switch]$PassThru,

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

    begin {
        $ErrorActionPreference = 'Stop'
    }
    
    process {
        if ($PSCmdlet.ParameterSetName -eq 'ById') {
            $InputObject = [pscustomobject]@{
                'Base ID' = $BaseIdentity
                'Table' = $Table
                'Record ID' = $Id
            }
        }
        $uri = BuildUriString -BaseId $InputObject.'Base ID' -Table $InputObject.Table -RecordId $InputObject.'Record ID'

        $invParams = @{
            Uri      = $uri
            Method   = 'PATCH'
            HttpBody = @{
                'fields' = $Fields
                'typecast' = $True
            }
        }
        if ($PSBoundParameters.ContainsKey('ApiKey')) {
            $invParams.ApiKey = $ApiKey
        }

        $targetMsg = "AirTable Record ID [$($InputObject.'Record ID')] in table [$($InputObject.Table)]"
        $actionMsg = "Update fields [$($Fields.Keys -join ',')] to [$($Fields.Values -join ',')]"
        if ($PSCmdlet.ShouldProcess($targetMsg, $actionMsg)) {
            if ($PassThru.IsPresent){
                InvokeAirTableApiCall @invParams
            } else {
                InvokeAirTableApiCall @invParams | Out-Null
            }
        }
    }
}