Public/Get-AirTableApiKey.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
function Get-AirTableApiKey {
    <#
        .SYNOPSIS
            Queries the API key configuration to return the API key set earlier via the Save-AirTableApiKey command.
    
        .EXAMPLE
            PS> Get-AirTableApiKey

            This example pulls the API key from the configuration file.

    
    #>

    [CmdletBinding()]
    param
    (
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$ApiKey
    )
    
    $ErrorActionPreference = 'Stop'

    function decrypt([string]$TextToDecrypt) {
        $secure = ConvertTo-SecureString $TextToDecrypt
        $hook = New-Object system.Management.Automation.PSCredential("test", $secure)
        $plain = $hook.GetNetworkCredential().Password
        return $plain
    }

    try {
        if ($PSBoundParameters.ContainsKey('ApiKey')) {
            $script:AirTableApiKey = $ApiKey
            $ApiKey
        } elseif (-not ($encApiKey = (Get-PSAirTableConfiguration).Application.ApiKey)) {
            throw 'No API key found in configuration.'
        } else {
            $atKey = decrypt $encApiKey
            $script:AirTableApiKey = $atKey
            $script:AirTableApiKey
        }
    } catch {
        $PSCmdlet.ThrowTerminatingError($_)
    }
}