0
我有一段代碼,我設法使工作,但我覺得它可以寫得更容易。 Im新的PowerShell,並試圖更好地理解它。我在下面有一個雙foreach來獲取來自TFS REST-API調用的PSCustomObject的關鍵和值。PowerShell TFS REST-API對象循環建議
由於某種原因,我做了2個循環,但我不明白爲什麼這是必需的。
的$ nameCap.userCapabilities內容的樣本是
Name1 Name2
----- -----
Value1 Value2
所以基本上我要循環的「名/值對」,並得到他們的價值觀。
我能做些什麼更好?
$uri = "$tfsUri/_apis/distributedtask/pools/$global:agentPoolId/agents?api-version=3.0-preview&includeCapabilities=true"
$result = (Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -UseDefaultCredentials).value | select name, userCapabilities, systemCapabilities
#Loop over all agents and their capablities
foreach ($nameCap in $result)
{
$capabilityNamesList = New-Object System.Collections.ArrayList
#Loop over all userCapabilities and store their names
@($nameCap.userCapabilities) | %{
$current_Cap = $_
$req_cap_exists = $false
Get-Member -MemberType Properties -InputObject $current_Cap | %{
$temp_NAME = $_.Name
$temp_Value = Select-Object -InputObject $current_Cap -ExpandProperty $_.Name
[void]$capabilityNamesList.Add($temp_NAME)
}
}
}