官术网_书友最值得收藏!

  • Practical Network Automation
  • Abhishek Ratan
  • 200字
  • 2021-07-02 14:53:09

API access

Here, we call the weather API to get coordinates for a particular location (say London, England):

#use the city of london as a reference
$city="london"
$urlx="https://samples.openweathermap.org/data/2.5/weather?q="+$city+"&appid=b6907d289e10d714a6e88b30761fae22"
# used to Invoke API using GET method
$stuff = Invoke-RestMethod -Uri $urlx -Method Get
#write raw json
$stuff
#write the output of latitude and longitude
write-host ("Longitude: "+$stuff.coord.lon+" , "+"Latitude: "+$stuff.coord.lat)

The output is as follows:

PS C:\Users\abhishek.ratan> C:\gdrive\book2\github\edition2\chapter1\api_invoke_ps.ps1
coord : @{lon=-0.13; lat=51.51}
weather : {@{id=300; main=Drizzle; description=light intensity drizzle; icon=09d}}
base : stations
main : @{temp=280.32; pressure=1012; humidity=81; temp_min=279.15; temp_max=281.15}
visibility : 10000
wind : @{speed=4.1; deg=80}
clouds : @{all=90}
dt : 1485789600
sys : @{type=1; id=5091; message=0.0103; country=GB; sunrise=1485762037; sunset=1485794875}
id : 2643743
name : London
cod : 200
Longitude: -0.13 , Latitude: 51.51

As we can see in the code, a major difference between writing code in Python and PowerShell is that in PowerShell we do not need to focus on indentation. PowerShell does not care about indentation, whereas a Python compilation would fail if strict indentation was not adhered to.

Also, we do not need to import any specific library in PowerShell, as it has very extensive built-in functions that are directly callable from the script. 

主站蜘蛛池模板: 枣强县| 高尔夫| 达尔| 额敏县| 台湾省| 信宜市| 河间市| 钟山县| 朝阳县| 新兴县| 宜君县| 浙江省| 岳池县| 正定县| 江山市| 南华县| 津南区| 塔河县| 富民县| 阿克苏市| 东乌| 高尔夫| 手机| 平泉县| 石景山区| 铁岭县| 高安市| 赣州市| 长沙县| 蕲春县| 泰安市| 太和县| 双鸭山市| 苏尼特右旗| 宁乡县| 曲阳县| 保靖县| 应城市| 阿拉善左旗| 梁河县| 深水埗区|