Scripts/Get-CoinbaseCurrencyConversion.ps1


Function Get-CoinbaseCurrencyConversion {

    <#
 
    .SYNOPSIS
 
    Converts funds from currency to currency.
 
    .DESCRIPTION
 
    Converts funds from currency to currency.
 
    .PARAMETER From
 
    Currency to be converted from
 
    .PARAMETER To
 
    Currency to be converted to
 
    .EXAMPLE
 
    Get-CoinbaseCurrencyConversion -From BTC -To USD
 
    .NOTES
 
    .LINK
 
    #>


    [CmdletBinding ()]

    Param ()

    DynamicParam {

        $ParameterNameFromCurrency = 'From'
        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
        $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
        $ParameterAttribute.Mandatory = $True
        $ParameterAttribute.HelpMessage = 'Enter from currency'
        $ParameterAttribute.Position = 0
        $AttributeCollection.Add($ParameterAttribute)
        $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        $ArraySet = Get-Content -Path ("$env:ALLUSERSPROFILE\Coinbase\Currencies.txt")
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($ArraySet)
        $AttributeCollection.Add($ValidateSetAttribute)
        $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterNameFromCurrency, [String], $AttributeCollection)
        $RuntimeParameterDictionary.Add($ParameterNameFromCurrency, $RuntimeParameter)

        $ParameterNameToCurrency = 'To'
        $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
        $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
        $ParameterAttribute.Mandatory = $True
        $ParameterAttribute.HelpMessage = 'Enter to currency'
        $ParameterAttribute.Position = 1
        $AttributeCollection.Add($ParameterAttribute)
        $ArraySet = Get-Content -Path ("$env:ALLUSERSPROFILE\Coinbase\Currencies.txt")
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($ArraySet)
        $AttributeCollection.Add($ValidateSetAttribute)
        $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterNameToCurrency, [String], $AttributeCollection)
        $RuntimeParameterDictionary.Add($ParameterNameToCurrency, $RuntimeParameter)

        Return $RuntimeParameterDictionary

    }

    BEGIN {

        $FromCurrency = ($PSBoundParameters[$ParameterNameFromCurrency]).Trim()
        $ToCurrency = ($PSBoundParameters[$ParameterNameToCurrency]).Trim()

        Function Show-Output ($Values) {

            [PSCustomObject]@{

                From = $FromCurrency.ToUpper()
                To = $ToCurrency.ToUpper()
                Rate = $Values
                Status = $Status

            }

        }

    }

    PROCESS {

        Try {

            $Headers = New-Object 'System.Collections.Generic.Dictionary[[String],[String]]'
            $Headers.Add('Content-Type', 'application/json')

            $Response = Invoke-RestMethod "https://api.coinbase.com/v2/exchange-rates?currency=$FromCurrency" -Method 'GET' -Headers $Headers

            $Status = 'Ok'

            Show-Output ($Response.Data.Rates.$ToCurrency)

        }

        Catch {

            $Status = $PSItem.Exception.Message

            Show-Output ('')

        }

    }

    END {}

}