2017-03-09 74 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