2017-03-09 59 views
0

我想使用PowerShell查询Web服务并请求我的城市(德国汉堡)的天气进行演示。PowerShell - 天气WebService

我从2011年发现了以下URL和脚本。但是这个Web服务不再工作了。

任何想法来解决这个问题?

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') 

响应:无数据

+2

OpenWeatherAPI呢?看到这里例如:http://www.powershelladmin.com/wiki/Get-Weather_cmdlet_for_PowerShell,_using_the_OpenWeatherMap_API –

+0

谢谢。我知道这个网站,但我想使用内置功能。 – LaPhi

回答

0

OpenWeather过来,并让自己的API关键。

This URL有一个所有城市的列表。有多个条目。我发现谷歌搜索和打这个URL旧金山(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