2017-07-24 96 views
1

我對Powershell和API調用頗爲陌生。Invoke-RestMethod在多行上顯示結果

我想從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

謝謝James, 我在代碼下面添加了圖片。對於PowerShell的結果和原始的API數據。 – tde

回答

0

我相信的樣子寫主機處理一個數組是這裏的問題,看看這個:

輸出:

$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 
} 
+0

這真的很有幫助,謝謝! – tde