Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[NoBrainer] PowerShell Currency Converter
# http://d-fens.ch/2014/01/07/nobrainer-powershell-currency-converter/
Param (
[Parameter(Mandatory = $true, Position = 0)]
[string] $From
,
[Parameter(Mandatory = $true, Position = 1)]
[string] $To
,
[Parameter(Mandatory = $false, Position = 2)]
[Alias("Amount")]
[double] $Value = 1
,
[Parameter(Mandatory = $false, Position = 3)]
[Alias("When")]
[DateTime] $Date = [datetime]::Now
) # Param
$UrlTemplate = 'http://www.oanda.com/currency/converter/update?base_currency_0={0}&quote_currency={1}&end_date={2}&view=details&id=1&action=C&';
[Uri] $Url = $UrlTemplate -f $From, $To, $Date.ToString('yyyy-MM-dd');
$r = Invoke-RestMethod -Uri $Url;
if($PSBoundParameters.ContainsKey('Value')) {
($r.data.bid_ask_data | gm -Type Properties).Name | % { $r.data.bid_ask_data.$_ *= $Value; };
for($c = 0; $c -lt $r.data.chart_data.Count; $c++) { $r.data.chart_data[$c][-1] *= $Value; };
} # if
$Currency = [Ordered] @{};
($r.data.bid_ask_data | gm -Type Properties).Name | % { $Currency.Add($_, $r.data.bid_ask_data.$_); };
# for($c = 0; $c -lt $r.data.chart_data.Count; $c++) {
# $dt = New-Object Datetime($r.data.chart_data[$c][0], $r.data.chart_data[$c][1], $r.data.chart_data[$c][2]);
# $Currency.Add($dt.ToString('yyyy-MM-dd'), $r.data.chart_data[$c][-1]);
# } # for
return $Currency;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment