Ryvik
Ryvik

Reputation: 463

Two ForEach loops, last one not iterating properly

Essentially, i'm using az module to gather a list of keyvault IDs then dump those vaults. However, the second ForEach loop is only using the last entry in the variable that was obtained from the first ForEach loop.

If ($ListAllSecrets)
{
    $ErrorActionPreference = "SilentlyContinue"
    Write-Host "Gathering all keys from key vaults, this may take a moment"
    $vaults=az keyvault list --query '[].name' -o tsv
    ForEach ($vault in $vaults){
        $ids = az keyvault secret list --vault-name $vault --query '[].id' -o tsv
        $ids

        }
    ForEach ($i in $ids){
        $i
        az keyvault secret show --id $i | ConvertFrom-Json
        }

Upvotes: 0

Views: 142

Answers (1)

Joy Wang
Joy Wang

Reputation: 42043

It should be like below.

If ($ListAllSecrets)
{
    $ErrorActionPreference = "SilentlyContinue"
    Write-Host "Gathering all keys from key vaults, this may take a moment"
    $vaults=az keyvault list --query '[].name' -o tsv
    ForEach ($vault in $vaults){
        $ids = az keyvault secret list --vault-name $vault --query '[].id' -o tsv
        $ids
             ForEach ($i in $ids){
                $i
                az keyvault secret show --id $i | ConvertFrom-Json
            }
        } 
}

Upvotes: 1

Related Questions