Advertisement
Guest User

powershellweather

a guest
Oct 30th, 2018
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. How the script should work:
  3. 1. The script asks the user to either write "GBG" (for Gothenburg) or "STO" (for Stockholm)
  4. 2. Whether the user types "GBG" or "STO" the $location variable will be set to 'GBG' or 'STO'. The question will loop if they type anything else
  5. 3. If $location equals 'GBG' or 'STO' the function "Get-Weather" should run
  6. 4. Depending on $location the script sets the path to the XML file with $XMLPath
  7. 5. The script should then check if the XML file exists or if it doesn't, and sets either $XMLExists to True or False
  8. 6. Now it should start executing the functions. If the XML already exists it should check if it is more than 15 minutes old, and if so update it.
  9.    If the XML doesn't exist it should download the XML from the API.
  10. 7. Now it should read the XML file with the function "ReadXML" and then write the actual weather to the user with function "WriteResultsToConsole"
  11. #>
  12.  
  13.  
  14.  
  15. Set-ExecutionPolicy unrestricted
  16.  
  17. Clear-Variable location
  18.  
  19.  
  20. function Get-Weather {
  21.  
  22.     #checks what location the user specified and sets the path to the XML file
  23.     if ($location = 'GBG') {$XMLPath = 'c:\outputgbg.xml'}
  24.     if ($location = 'STO') {$XMLPath = 'c:\outputsto.xml'}
  25.  
  26.     #Checks if the XML file already exists
  27.     write-host  $location
  28.     write-host $XMLPath
  29.     if(Test-Path -Path $XMLPath) {$XMLExists = $true}
  30.     else {$XMLExists = $false}
  31.     write-host $XMLExists
  32.  
  33.     #Updates XML for Gothenburg if it is more than 15 minutes old
  34.     function GBGCheckAndUpdateAlreadyExistingXML {
  35.         $filelastwrite = (get-item $XMLPath).LastWriteTime
  36.         $timespan = new-timespan -minutes 15
  37.         $timenow = (get-date -Format g)
  38.         if (((get-date) - $filelastWrite) -gt $timespan) {
  39.         write-host "`nThe weatherdata is old, fetching new file..."
  40.         Invoke-RestMethod -uri https://www.yr.no/sted/Sverige/V%C3%A4stra_G%C3%B6taland/G%C3%B6teborg/varsel.xml -outfile $XMLPath}
  41.         else {write-host "Weatherdata is up to date, no update required"}
  42.     }
  43.  
  44.     #Updates XML for Stockholm if it is more than 15 minutes old
  45.     function STOCheckAndUpdateAlreadyExistingXML {
  46.         $filelastwrite = (get-item $XMLPath).LastWriteTime
  47.         $timespan = new-timespan -minutes 15
  48.         $timenow = (get-date -Format g)
  49.         if (((get-date) - $filelastWrite) -gt $timespan) {
  50.         write-host "`nThe weatherdata is old, fetching new file..."
  51.         Invoke-RestMethod -uri https://www.yr.no/sted/Sverige/Stockholm/Stockholm/varsel.xml -outfile $XMLPath}
  52.         else {write-host "Weatherdata is up to date, no update required"}
  53.     }
  54.  
  55.     #Downloads XML for Gothenburg if the script runs for the first time
  56.     function GBGDownloadXMLIfNotExist {
  57.         Invoke-RestMethod -uri https://www.yr.no/sted/Sverige/V%C3%A4stra_G%C3%B6taland/G%C3%B6teborg/varsel.xml -outfile $XMLPath
  58.     }
  59.    
  60.     #Downloads XML for Stockholm if the script runs for the first time
  61.     function STODownloadXMLIfNotExist {
  62.         Invoke-RestMethod -uri https://www.yr.no/sted/Sverige/Stockholm/Stockholm/varsel.xml -outfile $XMLPath
  63.     }  
  64.  
  65.     #Sleep before reading XML
  66.     Start-Sleep -s 1
  67.  
  68.     #Reads XML file and sets variables
  69.     function ReadXML {
  70.         [xml]$xml = Get-Content -path $XMLPath
  71.  
  72.         $temperature = $xml.weatherdata.forecast.tabular.time[0].temperature.value
  73.         $weather = $xml.weatherdata.forecast.tabular.time[0].symbol.name
  74.         $weathertomorrow = $xml.weatherdata.forecast.tabular.time[4].symbol.name
  75.         $sunrise = $xml.weatherdata.sun.rise
  76.         $sunset = $xml.weatherdata.sun.set
  77.        
  78.     }
  79.  
  80.     #Writes information to the console
  81.     function WriteResultsToConsole {
  82.         $clock = (get-date -Format g)
  83.         write-host "`nThe date is" $clock "and the weather is" $weather "and the temperature is" $temperature "°C"
  84.         write-host "The sunrise is at" $sunrise "and the sunset is at" $sunset
  85.  
  86.         write-host "Tomorrow the weather will be" $weathertomorrow
  87.         }
  88.  
  89.  
  90.  
  91.  
  92.  
  93. #Executes the functions
  94. if ($XMLExists = $true -And $location -eq 'GBG') {GBGCheckAndUpdateAlreadyExistingXML}
  95. if ($XMLExists = $true -And $location -eq 'STO') {STOCheckAndUpdateAlreadyExistingXML}
  96. if ($XMLExists = $false -And $location -eq 'GBG') {GBGDownloadXMLIfNotExist}
  97. if ($XMLExists = $false -And $location -eq 'STO') {STODownloadXMLIfNotExist}
  98. ReadXML
  99. WriteResultsToConsole
  100.  
  101. }
  102.  
  103.  
  104. do {
  105. Clear-Variable -name location
  106. write-host $location
  107. $location = Read-host -prompt "Choose your location, write either GBG or STO"
  108. write-host $location
  109. if ($location -eq 'GBG' -or $location -eq 'STO') {Get-Weather}
  110. else {write-host "Please choose GBG or STO"}
  111. write-host $location
  112.  
  113. }
  114. until (($location -eq "GBG") -or ($location -eq "STO"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement