Friday, December 1, 2017

Get Bitcoin Price with Powershell

With this function you can get the Bitcoin price as a string value or spoken with Powershell

function Get-BitcoinPrice{

    param (
    [Boolean]$Speech = $false
    )
    
    #Get the current price from coinmarketcap.com
    $content = Invoke-WebRequest -uri https://api.coinmarketcap.com/v1/ticker/bitcoin/
    
    #Select the price from the content
    $found = $content.Content -match "price_usd`": `"(?<price>.*)`"," 

    $price = $matches['price'].ToString()

    #If the parameter -Speech is $true, then you can hear a voice saying the price.
    if($Speech -eq $true){
        Add-Type -AssemblyName System.speech
        $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer

        $speak.Speak("Bitcoin price is $price")
    }

    #The price is returned for future usage.
    Return $price
}

Get-BitcoinPrice -Speech $true

# or

Get-BitcoinPrice