Public/Get-EuroExchange.ps1

# ? TITEL Get-EuroExchange
# ? DESCRIPTION Rechnet Euros in Nicht-Euro-Währungen gem. den Vorgaben der EZB.
# ? TAGS UserCmdlet XML Http EZB
# ? VERSION 2019.01.01

using namespace System
using namespace System.Runtime.InteropServices
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
Set-StrictMode -Version Latest

function Get-EuroExchange {
    <#
    .SYNOPSIS
        EZB-Währungsrechner
    .DESCRIPTION
        Rechnet Euros in Nicht-Euro-Währungen gem. den Vorgaben der EZB.
    .EXAMPLE
        Get-EuroExchange -Currency USD
        Ermittelt den Wechselkurs für US-Dollar
    .EXAMPLE
        Get-EuroExchange -Currency USD -Euros 100
    .EXAMPLE
        Get-EuroExchange -ListCurrency
    .EXAMPLE
        "USD", "RUB", "AUD" | Get-EuroExchange
    .EXAMPLE
        "USD", "RUB", "AUD" | Get-EuroExchange -Euros 100
    .EXAMPLE
        "USD,10", "RUB,100", "AUD,1000" | ConvertFrom-Csv -Header Currency, Euros | Get-EuroExchange
    .INPUTS
        System.String mit Währungssymbolen
        PSCustomObject mit folgenden Properties Currency <string> und Euros <decimal>
    .OUTPUTS
        PSCustomObject
    .PARAMETER Currency
        Währungssymbol der EZB.
    .PARAMETER Euros
        Euro-Betrag der umgerechnet werden soll.
    .PARAMETER ListCurrency
        Eine Übersicht aller möglichen Währungssymbole.
    #>

    [CmdletBinding(DefaultParameterSetName='Calculate')]
    param (
        [Parameter(ParameterSetName                = "Calculate", 
                   Mandatory                       = $true, 
                   ValueFromPipeline               = $true, 
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateSet('USD','JPY','BGN','CZK','DKK','GBP','HUF','PLN','RON','SEK','CHF','ISK','NOK','HRK','RUB','TRY', `
                     'AUD','BRL','CAD','CNY','HKD','IDR','ILS','INR','KRW','MXN','MYR','NZD','PHP','SGD','THB','ZAR')]
        [Alias("Währung")]
        [string]$Currency,
        
        [Parameter(ParameterSetName                = "Calculate", 
                   ValueFromPipelineByPropertyName = $true)]
        [ValidateRange(0.0001, 1000000)]
        [Alias("Euronen")]
        [decimal]$Euros = 1,
        
        [Parameter(ParameterSetName = "Overview", Mandatory=$true)]
        [switch]$ListCurrency
    )
    begin {
        [xml]$content = Invoke-WebRequest -Uri "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml" | Select-Object -ExpandProperty Content 
        $cubes = $content.Envelope.Cube.Cube.Cube
        if($PSCmdlet.ParameterSetName -eq "Overview") {
            $cubes | ForEach-Object -Process {
                return [PSCustomObject]@{Currency = $_.currency}
            } | Sort-Object -Property Currency
        }
    }
    process {
        if($PSCmdlet.ParameterSetName -eq "Calculate") {
            [decimal]$rate = $cubes | Where-Object -Property currency -EQ -Value $Currency | Select-Object -ExpandProperty rate
            return [PSCustomObject]@{
                Currency    = $Currency.ToUpper()
                Rate        = $rate
                Euros       = $Euros
                SumCurrency = $rate * $Euros
            }
        }
    }
    end {
        Remove-Variable -Name content, cubes, rate -Force -ErrorAction Ignore
    }
}
<# ! UTest: Feature-Umfang
Get-EuroExchange -Currency USD
Get-EuroExchange -Währung USD
Get-EuroExchange -Currency USD -Euros 100
Get-EuroExchange -ListCurrency
Get-EuroExchange
"USD", "RUB", "AUD" | Get-EuroExchange
"USD", "RUB", "AUD" | Get-EuroExchange -Euros 100
"USD,10", "RUB,100", "AUD,1000" | ConvertFrom-Csv -Header Currency, Euros | Get-EuroExchange
"USD,10", "RUB,100", "AUD,1000" | ConvertFrom-Csv -Header Currency, Euros | Get-EuroExchange | Where-Object SumCurrency -GE 1000
"USD,10", "RUB,100", "AUD,1000" | ConvertFrom-Csv -Header Currency, Euros | Get-EuroExchange | Out-GridView
#"USD,10", "RUB,100", "AUD,1000" | ConvertFrom-Csv -Header Währung, Euronen | Get-EuroExchange
Get-EuroExchange -ListCurrency | Get-EuroExchange -Euros 1000
Get-Help -Name Get-EuroExchange -ShowWindow
Show-Command -Name Get-EuroExchange -NoCommonParameter -ErrorPopup | Out-GridView
#>

<# ! UTEST: Validierung
Get-EuroExchange # Currency mus abgefragt werden
Get-EuroExchange -Currency USD -ListCurrency # Diese Parameter-Kombination ist nicht erlaubt
Get-EuroExchange -Euros 100 -ListCurrency # Diese Parameter-Kombination ist nicht erlaubt
Get-EuroExchange -Currency XXX # Gibt es nicht
Get-EuroExchange -Currency USD -Euros -100 # Negative Euros sind nicht erlaubt
Get-EuroExchange -Currency USD -Euros 1000001 # Größer als der max. Bereich.
Get-EuroExchange -Currency USD -Euros 0 # 0 Euros sind nicht erlaubt
Get-EuroExchange -Currency USD -Euros hundert # Nur Zahlen sind erlaubt
Get-EuroExchange -Euros 100 # Ohne Währungssymbol nicht erlaubt
Get-EuroExchange -Currency USD, RUB # Zuviel Währungen
Get-EuroExchange -Currency USD -Euros 50, 100 # Zuviel Euros
#>