# prtg-openweathermap # # http://openweathermap.org/appid # https://openweathermap.org/?q=hövelhof,de # [cmdletbinding()] param ( $apiurl="http://api.openweathermap.org/data/2.5/weather?", $cityid="6553131", # einfach auf http://openweathermap.org/city/ den eigenen Ort suchen und dann die ID aus der URL auslesen # oder aus der Liste von http://bulk.openweathermap.org/sample/city.list.json.gz aussuchen $apikey="11111111111111111111111111111111", $cachefilename = ".\prtg-openweathermap.xml", $historyfilename = ".\prtg-openweathermap.csv" ) if ($PSScriptRoot.EndsWith("EXEXML")) { write-verbose "Check-ADFS: Assume PRTG EXEXML-Mode" } else { Set-PSDEBUG -strict # enable strict variable checking. Not usable with PRTG $VerbosePreference = "continue" } write-verbose "PRTG-OpenWeatherMap: Start" write-verbose " Parameter: apiurl : $($apiurl)" write-verbose " Parameter: cityid : $($cityid)" write-verbose " Parameter: apikey : $($apikey)" write-verbose " Parameter: Cachfilename: $($cachefilename)" write-verbose "Reading Filedate of cachefile $($cachefilename)" $error.clear() $cachefile = get-item -path $cachefilename -erroraction silentlycontinue if ($error) { write-verbose "- No LastWrite. set to now -11 Minutes" $lastwritetime = (get-date).addminutes(-11) $error.clear() } else { write-verbose "- Cachfile found" $lastwritetime = ($cachefile).lastwritetime } write-verbose "- LastWriteTime: $($lastwritetime)" if (((get-date) - $lastwritetime).totalminutes -lt 10) { write-verbose "- Reading data from cache due 10 Min Interval" $result = import-clixml $cachefilename } else { write-verbose "- Loading data from OpenWeatherMap" write-verbose "- Building APIURL" # http://api.openweathermap.org/data/2.5/forecast?id=524901&APPID=1111111111 # http://samples.openweathermap.org/data/2.5/forecast?id=524901&appid=b1b15e88fa797225412429c1c50c122a1 $url = $apiurl+"id=$($cityid)&APPID=$($apikey)" write-verbose " URL = $($url)" try { write-verbose "- Starting Rest-Query" $result = invoke-restmethod -uri $url write-verbose " Write Data to CacheFile $($cachefilename)" $result | export-clixml $cachefilename } catch { write-verbose " Error fetching data from openweathermap" $result = $null } } if ($result) { write-verbose "- Processing Results" write-verbose " Timestamp $($result.dt_txt)" if ($historyfilename) { Write-Verbose " Append History CSV" $csventry= ""| select timestamp,temp,pressure,humiditiy,cloudiness $csventry.timestamp=$result.dt_txt $csventry.temp=([decimal]($result.main.temp) - 273.15) $csventry.pressure=$result.main.pressure $csventry.humiditiy=$result.main.humiditiy $csventry.cloudiness=$result.clouds.all $csventry | export-csv -Path $historyfilename -Append -NoTypeInformation } #$result $prtgresult = ' temp C '+([decimal]($result.main.temp) - 273.15)+' Grad C 1 pressure mBar '+$result.main.pressure+' mBar humiditiy (%) '+$result.main.humidity+' % Cloudiness (%) '+$result.clouds.all+' % LastUpdate'+$result.dt_txt+' ' write-verbose "Send PRTG-Result to STD-Out" $prtgresult } else { write-verbose "- no Results to process" Write-Output "" Write-Output "1" Write-Output "Error receiving data from URL or Cchefile" Write-Output "" Exit } write-verbose "PRTG-OpenWeatherMap: End"