Private/GetBaseId.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
function GetBaseId {
    <#
        .SYNOPSIS
            A helper function to query the configuration with a base name and return it's ID.
    
        .EXAMPLE
            PS> GetBaseId -Identity foo

            Looks at the module configuration file for a base name defined as 'foo' and returns the ID associated with it.
    
    #>

    [OutputType('string')]
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$Identity
    )

    $ErrorActionPreference = 'Stop'

    $baseIdtype = GetBaseIdentityType -Identity $Identity
    if ($baseIdtype -eq 'Name') {
        $bases = (Get-PSAirTableConfiguration).Bases
        if (-not ($base = @($bases).where({ $_.Name -eq $Identity }))) {
            throw "The base name [$($Identity)] could not be found. Ensure it exists by running (Get-PSAirTableConfiguration).Bases"
        }
        $base.id
    } else {
        $Identity
    }
}