Untitled

From random_gangsta, 2 Years ago, written in PowerShell, viewed 7 times.
URL https://pastebin.h0me.dk/view/f883c981 Embed
Download Paste or View Raw
  1. . C:\Scripts\write-log.ps1
  2. $logfile = "C:\Scripts\logs\spotprices.log"
  3. Write-Log -path $logFile -level Info -tag Script -msg "Script starting"
  4.  
  5. # Database stufF:
  6. Import-Module SimplySql
  7. $dbUsername = "usrname"
  8. $dbPass = "password"
  9. $database = "database"
  10. $sqlHost = "sqlhost"
  11.  
  12. Write-Log -path $logFile -level Info -tag Script -msg "Opening MySQL Connection"
  13. Open-MySqlConnection -Server $sqlHost -Database $database -UserName $dbUsername -Password $dbPass -ConnectionName "graph"
  14.  
  15. $curTimezone = (Get-Date -Format tz).Replace("+","-")
  16. $start = $(Get-Date((Get-Date).AddDays(1)) -Format yyyy-MM-dd)
  17. $end = $(Get-Date((Get-Date).AddDays(2)) -Format yyyy-MM-dd)
  18.  
  19. Write-Log -path $logFile -level Info -tag Script -msg "curTimezone: $curTimezone`tStart: $start`tEnd:`t$end"
  20.  
  21.  
  22. $newUrl = "https://api.energidataservice.dk/dataset/Elspotprices?offset=0&start=$($start)T00:00&end=$($end)T00:00&filter=%7B%22PriceArea%22:[%22DK1%22]%7D&sort=HourUTC%20DESC&timezone=dk"
  23. $response = $null
  24. try{
  25.     Write-Log -path $logFile -level Info -tag WebRequest -msg "Calling energidataservice.dk"
  26.     $response = Invoke-RestMethod $newUrl -Method 'GET'
  27. }catch {
  28.     Write-Host "fucked up"
  29.     Write-Host $Error
  30.     Write-Log -path $logFile -level Error -tag WebRequest -msg "Failed with calling energidataservice.dk"
  31. }
  32.  
  33. $count = 0
  34. foreach ($i in $response.records){
  35.     $count++
  36.     # Er udelukkende fordi PS og grafana bruger den tidszone man i er ift unixtime stmap. Dvs
  37.     # at den unixtimestmap der REELT st�r for en time, ikke er den. Alts� det er HourDK der passer og det
  38.     # er s� lagt en time til unixtimestmap f�r det passer.. Tror jeg.. H�ber jeg.. Passer nu i grafana og set-.temp!
  39.     # okay jeg fatter faktisk ikke helt hvorfor det passer... men det passer, WHO CARES!
  40.     $ourTime = (Get-date($i.HourDK)).addhours($curTimezone)
  41.     #$ourTime = (Get-date($i.HourDK))
  42.     $spotHourUnix = Get-date($ourTime) -UFormat %s -Millisecond 0
  43.     $unixtimestamp = $spotHourUnix
  44.     $kwpricedkk = [math]::Round($i.SpotPriceDKK / 1000,2)
  45.     $HourUTC = $i.HourUTC
  46.     $HourDK = $i.HourDK
  47.     $SpotPriceDKK = [math]::Round(($i.SpotPriceDKK),2)
  48.  
  49.     Write-Log -path $logFile -level Info -tag Data -msg "unixtimestamp: $unixtimestamp`tkwpricedkk: $kwpricedkk`tHourUTC: $HourUTC`tHourDK: $HourDK`tSpotPriceDKK: $SpotPriceDKK"
  50.     $query = "INSERT INTO elspotprices (id, unixtimestamp, kwpricedkk, HourUTC, HOURDK, SpotPriceDKK) VALUES (NULL, '$unixtimestamp', '$kwpricedkk','$HourUTC','$HourDK','$SpotPriceDKK')"
  51.     Write-Log -path $logFile -level Info -tag Data -msg "Running Query: $query"
  52.     Invoke-SqlQuery $query -ConnectionName "graph"
  53. }
  54. Write-Log -path $logFile -level Info -tag Data -msg "Count: $count`tStart: $start`tEnd: $End"
  55. Write-Log -path $logFile -level Info -tag Script -msg "Script ending"
  56.  
  57.  

Reply to "Untitled"

Here you can reply to the paste above

captcha