Just for fun, I thought it’d be cool to track the price of bitcoin using powershell (my boss is still into it). I found an API that returns everything I want using JSON format - and that works perfectly fine – have a look below if you’d like to replicate. My problem is, if I want to make it a “live ticker” - so to speak - I’m expecting to do something like Do {Get-BitcoinValue; Start-Sleep -Sec 60 } while ($true) however, the first Get-BitcoinValue doesn’t display anything… then after 60 seconds, I get the first and second results at the same time, then every 60 seconds a new “row” is added. I feel like this is something to do with how powershell handles streaming output to the host, but I have no idea how to workaround that. Curious if anyone has any thoughts.
$ApiURL = 'https://api.coindesk.com/v1/bpi/currentprice.json'
# you can leave out the below line if you don't want the headache of making a default view
#Update-FormatData "$($PSScriptRoot)\BitcoinObjectView.format.ps1xml"
function Get-BitCoinValue {
[cmdletBinding()]Param()
$data = Invoke-RestMethod -Uri $ApiUrl
$Time = ([datetime]$data.time.updatedISO).ToLocalTime()
$obj = New-Object -TypeName psobject -Property ([ordered]@{
DateTime = "$("{0:yyyy-MM-dd HH:mm:ss}" -f $Time.tolocaltime())"
Coin = $Data.ChartName
USD = ("{0:c}" -f [double]$data.bpi.usd.rate.replace(',',''))
EUR = ("$([char]0x20ac)" + ("{0:n2}" -f [double]$data.bpi.EUR.rate.replace(',','')))
GBP = ("$([char]0x00A3)" + ("{0:n2}" -f [double]$data.bpi.GBP.rate.replace(',','')))
})
$obj.pstypenames.insert(0,'BitCoinObject')
$obj
}
And if you’re curious about the default view:
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<ViewDefinitions>
<View>
<Name>BitcoinObjectTable</Name>
<ViewSelectedBy>
<TypeName>BitCoinObject</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>DateTime</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Coin</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>USD</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>EUR</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>GBP</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>