Using PowerShell to gather information from XML

Using PowerShell to gather information from XML

Making a long backstory short, I was checking one of our local weather sites during one of the few warm summer days here in Northern-Norway. For most people, it ends there but I obviously started thinking that it would be so much easier to write a function that could check the weather for me. I head off to check their API and it turns out that they are working on a modern API but is serving out XML files based on location.

I have never worked with XML before, so the journey to extract the information I wanted was kind of interesting. I noted everything, so let’s take a look at how one can go about extracting the information one needs from XML and use it in PowerShell. Feel free to follow the lines of code.

# Load the file, define it as XML.
$URL = "https://www.yr.no/sted/Norge/postnummer/9020/varsel.xml"
$file = Invoke-webrequest -uri $URL
# Check the result.
$file
# Check the property "weatherdata".
$file.weatherdata
# We want real time data, so let's continue with observations.
$file.weatherdata.observations
# Closing in at something. Checking out our options by using Get-Member...
$file.weatherdata.observations | Get-Member
# And now, weatherstation?
$file.weatherdata.observations.weatherstation
# Bingo. 

Leave a Comment