1

I'm creating a pwsh script for json population.

   $SqlServers="kc-emea-sqsrv-00","kc-emea-sqsrv-01","kc-emea-sqsrv-02","kc-emea-sqsrv-03"
   $SqlServerResourceGroup="KC-EMEA-RSGP-00","KC-EMEA-RSGP-01","KC-EMEA-RSGP-02","KC-EMEA-RSGP-03"

I'd like to access in sequence to each value in the above array.

    foreach ($SqlServer in $SqlServers) {
    az sql db list --resource-group $SqlServerResourceGroup --server $SqlServer --query "[].{name:name,value:name}" -o json *>  "C:\script\json\$SqlServers.json"
    }

How can I access in sequence at $SqlServerResourceGroup array variables in order to create a json for each server?

1 Answer 1

1

You should be using a for loop to achieve that:

For ($i=0; $i -lt $SqlServers.Length; $i++) {
   az sql db list --resource-group $SqlServerResourceGroup[$i] --server $SqlServers[$i] --query "[].{name:name,value:name}" -o json *>  "C:\script\json\$SqlServers.json"
}
Sign up to request clarification or add additional context in comments.

2 Comments

It worked. Is it possible also to have the Value in the final path of the query? -o json *> "c:\script\json\$SqlServers[$i].json" ? How can I escape square bracket in powershell?
Try "c:\script\json\$($SqlServers[$i]).json".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.