PowerShell can easily get information on the Web page and read the corresponding content. If the object's format is XML or JSON, it's easier to handle, often using the Invoke-restmethod and Invoke-webrequest commands. The first is to get the content of the JSON format, which can get the contents of the entire Web page.
For example, I would like to inquire about the weather in Sydney tomorrow. A Web site that provides apis is randomly searched.
Http://openweathermap.org/current#name
I'm going to search for Sydney, so the corresponding format is
Http://api.openweathermap.org/data/2.5/weather?q=sydney,au He will automatically generate a JSON-formatted result.
We can use Invoke-restmethod to get this result directly, for example
$b =invoke-restmethod "Http://api.openweathermap.org/data/2.5/weather?q=sydney,au"
$c =[pscustomobject]@{
' Description ' = $b. Weather.description
' name ' = $b. Name
' windspeed ' = $b. Wind.speed
I can also use Invoke-webrequest to crawl the content of the entire page, and then convert it from JSON to the same way.
$a = Invoke-webrequest-uri "Http://api.openweathermap.org/data/2.5/weather?q=sydney,au" $b = $a. Content | Convertfrom-json
Similarly, if I want to get the latest content of RSS for a blog. You can use Invoke-webrequest to crawl the corresponding XML file, such as
[XML] $a = Invoke-webrequest-uri "http://blogs.msdn.com/b/powershell/rss.aspx" $a. Rss.channel.Item | Select Title,pubdate
The function is very powerful, the use is very simple.
This article comes from the "Mapo Tofu" blog