1
我正在嘗試編寫一些PowerShell腳本來管理對api的調用,如果將結果存儲在變量中,我可以很好地對它們進行迭代,但是我想了解如何通過管道進行操作。在PowerShell中調用invoke-restmethod的結果
這工作
#This uri returns an array of objects that describe searches via the api
$reports = Invoke-RestMethod -uri $(root)/search -Method get -UseDefaultCredentials
foreach($r in $reports){
#can do stuff here
}
而且如果我這樣做,我得到的控制檯窗口
$reports = Invoke-RestMethod -uri $(root)/search -Method get -UseDefaultCredentials | select
顯示的結果,但我不能找到後使用效果的內容的一種方式管道
#nothing displayed in the console for this
$reports = Invoke-RestMethod -uri $(root)/search -Method get -UseDefaultCredentials | ForEach-Object { Write-Host $_.Id }
訪問管道結果中的項目的正確方法是什麼?
我的上帝,我是這樣的新手想念! –
此外,要注意的一個怪癖:有時會返回一個數組,但整個數組傳遞到管道(而不是單個項目)。將「Invoke-RestMethod」調用放在括號中可修復此問題。 [更多信息](https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/11201622-invoke-restmethod-returns-an-unrolled-array)。 – Vimes