Invoke-RestMethod在多行上显示结果

时间:2017-07-24 14:56:54

标签: rest api powershell

我对Powershell和API调用很新。

我正在尝试从Powershell调用BA API来返回BA处于活动状态的所有国家/地区名称。

我有代码返回我之后的结果,但所有结果都返回单行。我想在新行上显示每个结果。每个国家EG一行。

任何建议都将不胜感激。我的代码如下,但API密钥已被删除。

$uri = 'https://api.ba.com/rest-v1/v1/balocations'
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("client-key",'xxxxxxxxxx')

function FlightLocations($uri, $headers)
{
    Invoke-RestMethod -Method GET -Uri $uri -Headers $headers 
}

$response = FlightLocations $uri $headers 
Write-Host $response.GetBA_LocationsResponse.Country.CountryName 

Powershell Results
API Data

1 个答案:

答案 0 :(得分:0)

我相信Write-Host处理数组的方式就是问题所在,请看:

输出:

$numbers = @('1','2','3','4','5')

DataType Object[] array

Write-Host $numbers does not work
1 2 3 4 5

$numbers | Write-Host works
1
2
3
4
5

Using foreach works to
1
2
3
4
5

代码:

cls

#test data
$numbers = @('1','2','3','4','5')
"`$numbers = @('1','2','3','4','5')"
#Type of data
"
DataType $($numbers.gettype().Name) $($numbers.gettype().BaseType)"

#Does not work.
"
Write-Host `$numbers does not work"
Write-Host $numbers

#Works
"
`$numbers | Write-Host works"
$numbers | Write-Host


#Another aproach.
"
Using foreach works to"
Foreach ($number in $numbers)
{
    Write-Host $number
}