Powershell可以很輕松的獲取網(wǎng)頁(yè)的信息并讀取到對(duì)應(yīng)的內(nèi)容。如果對(duì)象的格式是XML或者Json,那就更容易處理了,一般經(jīng)常使用invoke-restmethod和invoke-webrequest這兩個(gè)命令。前者主要是獲取Json格式的內(nèi)容,后者可以獲取整個(gè)網(wǎng)頁(yè)的內(nèi)容。
比如說(shuō)我希望查詢明天悉尼的天氣如何。網(wǎng)上隨便搜了一個(gè)提供API的站點(diǎn)
http://openweathermap.org/current#name
我打算搜索悉尼的,那么對(duì)應(yīng)的格式是
http://api.openweathermap.org/data/2.5/weather?q=sydney,au他會(huì)自動(dòng)生成一個(gè)Json格式的結(jié)果。
![](/d/20211017/9bf917bf4022c1993beeb3acd6b29718.gif)
我們可以用invoke-restmethod直接獲取這個(gè)結(jié)果,比如說(shuō)
$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
}
![](/d/20211017/d19b3aa1333ddeebea589d93b3b8f7c0.gif)
我也可以直接使用invoke-webrequest抓取整個(gè)網(wǎng)頁(yè)的內(nèi)容,然后從Json的格式轉(zhuǎn)換過(guò)來(lái)也是一樣的
$a= Invoke-WebRequest -Uri "http://api.openweathermap.org/data/2.5/weather?q=sydney,au"$b=$a.Content | ConvertFrom-Json
類(lèi)似的,如果我想獲取一個(gè)博客的RSS的最新內(nèi)容??梢允褂胕nvoke-webrequest抓取對(duì)應(yīng)的XML文件,比如
[xml]$a= Invoke-WebRequest -Uri "http://blogs.msdn.com/b/powershell/rss.aspx“$a.rss.channel.Item | select title,pubdate
![](/d/20211017/194f910e13e4a3eb92d01866e5b5eb2c.gif)
功能很強(qiáng)大,使用卻很簡(jiǎn)單。
本文出自 “麻婆豆腐” 博客
您可能感興趣的文章:- Powershell 查詢 Windows 日志的方法
- Powershell 查找用戶的主SMTP地址
- powershell解決win10開(kāi)始菜單和通知中心無(wú)法打開(kāi)
- Powershell錯(cuò)誤處理之what-if
- PowerShell 4.0實(shí)現(xiàn)自動(dòng)化設(shè)置服務(wù)器
- 揭秘PowerShell 5.0新特性和新功能
- Windows 8 中的 PowerShell 3.0
- PowerShell使用小技巧分享
- 使用PowerShell修改注冊(cè)表
- PowerShell用戶認(rèn)證Function實(shí)例代碼