Converting Currencies

by Feb 9, 2016

PowerShell is an extremely powerful language and can access web services and web pages. If you combine that with dynamic parameters, you get a professional currency converter with real-time exchange rate support.

Here is the ConvertTo-Euro function that takes values from other currencies and converts them to EUR. The function has the -Currency parameter that is populated dynamically by the currencies supported by the European Central Bank.

function ConvertTo-Euro
{
  [CmdletBinding()]
  param(
    [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
    [Double]
    $Value
  )

  dynamicparam
  {
    $Bucket = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary

    $Attributes = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute]    
    $AttribParameter = New-Object System.Management.Automation.ParameterAttribute
    $AttribParameter.Mandatory = $true
    $Attributes.Add($AttribParameter)
    
    if ($script:currencies -eq $null)
    {
      $url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
      $result = Invoke-RestMethod  -Uri $url
      $script:currencies = $result.Envelope.Cube.Cube.Cube.currency
    }
    
    $AttribValidateSet = New-Object System.Management.Automation.ValidateSetAttribute($script:currencies)
    $Attributes.Add($AttribValidateSet)

    $Parameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter('Currency',[String], $Attributes)
    $Bucket.Add('Currency', $Parameter)

    $Bucket
  }

  begin
  {
    foreach ($key in $PSBoundParameters.Keys)
    {
      if ($MyInvocation.MyCommand.Parameters.$key.isDynamic)
      {
        Set-Variable -Name $key -Value $PSBoundParameters.$key
      }
    }
  
    $url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
    $rates = Invoke-RestMethod  -Uri $url
    $rate = $rates.Envelope.Cube.Cube.Cube | 
    Where-Object { $_.currency -eq $Currency} |
    Select-Object -ExpandProperty Rate
  }

  process
  {
    $result = [Ordered]@{
      Value = $Value
      Currency = $Currency
      Rate = $rate
      Euro = ($Value / $rate)
      Date = Get-Date
    }
    
    New-Object -TypeName PSObject -Property $result
  }
}

The function illustrates how a dynamic parameter can be populated by dynamic data, and how this data is cached so IntelliSense won't trigger a new retrieval all the time.

Here is some sample output you can expect (provided you have Internet access):

 
PS C:\> 100, 66.9 | ConvertTo-Euro -Currency DKK


Value    : 100
Currency : DKK
Rate     : 7.4622
Euro     : 13,4008737369677
Date     : 26.01.2016 21:32:44

Value    : 66,9
Currency : DKK
Rate     : 7.4622
Euro     : 8,96518453003136
Date     : 26.01.2016 21:32:45




PS C:\>  ConvertTo-Euro -Currency USD -Value 99.78


Value    : 99,78
Currency : USD
Rate     : 1.0837
Euro     : 92,0734520623789
Date     : 26.01.2016 21:33:01
 

 

Throughout this month, we'd like to point you to three awesome community-driven global PowerShell events taking place this year:

Europe: April 20-22: 3-day PowerShell Conference EU in Hannover, Germany, with more than 30+ speakers including Jeffrey Snover and Bruce Payette, and 60+ sessions: www.psconf.eu.

Asia: October 21-22: 2-day PowerShell Conference Asia in Singapore. Watch latest announcements at www.psconf.asia

North America: April 4-6: 3-day PowerShell and DevOps Global Summit in Bellevue, WA, USA with 20+ speakers including many PowerShell Team members: https://eventloom.com/event/home/PSNA16

All events have limited seats available so you may want to register early.

Twitter This Tip! ReTweet this Tip!