需要将API的输出存储到.csv文件中

时间:2019-07-02 06:27:28

标签: powershell

我无法将Invoke-WebRequest的值单行存储到.csv文件。

我想以以下格式将天气详细信息存储到管道分隔的.csv文件中:

TimeStamp|Temperature|RH

预期结果:

TIME_HK|HonkKong.Temperature|HonkKong.RH
$TIME = Get-Date
$TIME_HK = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($TIME, [System.TimeZoneInfo]::Local.Id, 'China Standard Time')
$UnixTimeStamp = [System.Math]::Truncate((Get-Date -Date $TIME_HK -UFormat %s))
$URL = "http://my.weather.gov.hk/wxinfo/json/one_json.xml?_=" + $UnixTimeStamp
$Weather = Invoke-RestMethod -Method Get -Uri $URL -UseBasicParsing
$HonkKong = $Weather.hko

Details of Actual and Expected results

1 个答案:

答案 0 :(得分:0)

$Weather.hko | Export-Csv "out.csv" 

如果需要,您可以通过-Delimiter ";"设置定界符。

 > Get-Content .\out.csv
 "BulletinTime","Temperature","RH","HomeMaxTemperature","HomeMinTemperature"
 "201907021440","29.1","86","32","26"

如果要附加信息,可以使用-Append开关。