How do I create a nested for each when running a azure cli command using Powershell?

460 views Asked by At

I have the following command that grabs a list of storage accounts and run the following command against them:

foreach($storage in Get-Content .\storageaccount.txt) {
   Write-Host $storage
   az storage logging show --account-name $storage
}

If I want to run this command:

az storage account management-policy show --account-name "StorageAccountName" --resource-group "RGName"

  1. How do I grab the Resource Group associated with that Storage Account and plug it into a variable?

  2. How do I construct the for each loop to loop through the Storage Account and the Resource Groups?

1

There are 1 answers

4
Jim On

Update

Ok, so you will need to take some initiative here, look at the PowerShell Modules provided to see what you can come up with to pull it out. I don't know how your data is stored or what you can query on, but it appears you can do wild card names, locations, and tags from the PowerShell Module for Azure Storage Get-AzResourceGroups. If that doesn't work, then try looking through the documentation for something that will work.

Old Reply with Invalid code

Is there a reason you don't want to use the [PowerShell module for Azure Storage][2]?

Using the module it would return an object (vs parsing text) and your code would be something like:

 foreach($Storage in Get-Content .\storageaccount.txt) {
    Write-Host $Storage
    $StorageAccount = Get-AzStorageAccount -Name $Storage #Requires more parameters

    Write-Host "Resource Group: $($StorageAccount.ResourceGroupName)"
}