Public/New-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
function New-Record {
    <#
        .SYNOPSIS
            Creates a new AirTable table record.
    
        .EXAMPLE
            PS> New-Record -BaseName foo -Table bar -Fields { 'Name' = 'new' }

            Creates a new record in the 'bar' table with a field Name value of 'new'.

        .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 new record's fields. 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)]
    param
    (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [Alias('BaseName', 'BaseId')]
        [string]$BaseIdentity,

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

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

        [switch]$PassThru,

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


    $ErrorActionPreference = 'Stop'

    $baseId = GetBaseId -Identity $BaseIdentity

    $uri = BuildUriString -BaseId $baseId -Table $Table

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

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