【问题标题】:PowerShell - Weather WebServicePowerShell - 天气网络服务
【发布时间】:2017-03-09 16:16:17
【问题描述】:

我想使用 PowerShell 查询 Web 服务并询问我所在城市(德国汉堡)的天气以进行演示。

我找到了 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')

回应:没有数据

【问题讨论】:

  • OpenWeatherAPI 怎么样?见这里的例子:powershelladmin.com/wiki/…
  • 谢谢。我知道这个网站,但我想使用内置功能。

标签: web-services powershell openweathermap


【解决方案1】:

前往OpenWeather 并为自己获取API 密钥。

This URL 有一个所有城市的列表。有多个条目。我通过谷歌搜索找到了旧金山的 (5391959) 并点击了这个 URL:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-11
    • 2012-01-13
    • 2015-11-09
    • 2023-04-08
    • 2018-10-29
    • 1970-01-01
    • 2021-03-06
    • 2023-04-11
    相关资源
    最近更新 更多