Windows PowerShell is a robust programming platform which has the capability to cover, by and large, everything a developer can imagine. In this article we’ll articulate a little example to get the weather update through RSS feed.
This example uses the PowerShell’s Invoke-RestMethod cmdlet to get weather information from BBC Weather update.
Invoke-RestMethod -Uri https://weather-broker-cdn.api.bbci.co.uk/en/observation/rss/6167865 | Format-Table -Property Title, Description, pubDate -Wrap -Autosize
Invoke-RestMethod cmdlet pulls in weather update for the location specified after the last forward-slash “/”, that is 6167865. The response is directed to Format-Table cmdlet with some of the fields to show up. “-Wrap” and “-Autosize” parameters ensure that the entire output is rendered instead of truncated output.
In order to get location code of your choice go to https://www.bbc.com/weather and type your desired city like Toronto in the Weather search-bar. You may be given multiple locations matching your search city, e.g. in this typical case you’ll see Toronto Canada, and Toronto United States. Click your desired choice, e.g, Toronto Canada and you’ll be presented with the complete weather information about Toronto, Canada. Just check the address bar of the browser, it will be like:
Note that the digits after the last forward slash are the location code of your selected city, as in this peculiar case they’ll be 6167865.
Now that you’ve got the location code, you’re all set to complete your url for RSS feed for Toronto. The url will be like the following:
and your complete Invoke-RestMethod for the weather feed will be like:
Invoke-RestMethod -Uri https://weather-broker-cdn.api.bbci.co.uk/en/observation/rss/6167865 | Format-Table -Property Title, Description, pubDate | Out-File -Encoding utf8 "F:\WeatherUpdate.txt"
Here’s another way of getting feed into a variable using System.Xml library:
[System.Xml.XmlElement]$userfile = Invoke-RestMethod -Uri https://weather-broker-cdn.api.bbci.co.uk/en/observation/rss/6167865 $userfile
The last command $userfile will display the captured feed on the screen. Again you can direct the feed from the variable to an output file, like the following:
$userfile | Out-File -Encoding utf8 "F:\WeatherUpdate.txt"
If you’re curious to learn more about Windows PowerShell techniques, following articles will prove to be more helpful:
Windows PowerShell – Basic Constructs
Windows PowerShell Technique: Obtain Network Data
Windows PowerShell Technique: Get CPU Information
Windows PowerShell Technique: Get Hard Drive Information