PowerShell - 天气WebService

时间:2017-03-09 16:16:17

标签: web-services powershell openweathermap

我想使用PowerShell查询Web服务并询问我所在城市(德国汉堡)的天气情况。

我从2011年发现了以下网址和脚本。但是这个网络服务不再有用了。

有什么想法可以解决这个问题吗?

https://learn-powershell.net/2011/02/15/retrieving-weather-information-from-a-web-service-provider/

$a = New-WebServiceProxy 'http://www.webservicex.net/globalweather.asmx?WSDL' 
$a.GetWeather('San Francisco','United States')

回复:无数据

1 个答案:

答案 0 :(得分:0)

转到OpenWeather并获得API密钥。

This URL列出了所有城市。有多个条目。我通过Googling找到了旧金山(5391959)并点击了这个网址:

http://openweathermap.org/city/5391959?utm_source=openweathermap&utm_medium=widget&utm_campaign=html_old

...我从中提取了5391959城市ID。

使用您的API密钥和城市ID,您可以使用以下两个命令以JSON格式获取天气:

$weather = Invoke-WebRequest -Uri 'api.openweathermap.org/data/2.5/weather?id=5391959&APPID=YOUR_API_KEY_HERE'
$weatherObject = $weather.Content | ConvertFrom-Json
$weatherObject # display to console

...会给你一个对象,如:

coord      : @{lon=-122.42; lat=37.77}
weather    : {@{id=804; main=Clouds; description=overcast clouds; icon=04n}}
base       : stations
main       : @{temp=285.91; pressure=1017; humidity=93; temp_min=283.15; temp_max=288.15}
visibility : 16093
wind       : @{speed=4.6; deg=240}
clouds     : @{all=90}
dt         : 1489134900
sys        : @{type=1; id=226; message=0.1525; country=US; sunrise=1489156026; sunset=1489198372}
id         : 5391959
name       : San Francisco
cod        : 200