Search code examples
azurepowershellazure-blob-storageazure-powershell

Showing all the blobs from multiple storage containers in Azure using powershell


I am trying to list all the blobs from various containers that i have in an Azure storage account using powershell So i run the below commands :

$storageAccountName = "contoso"
$resourceGroup = "Contoso-Rg"
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $storageAccountName).Value[0]
$context=New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountkey
$containerName =   Get-AzStorageContainer -Context $context

$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName

$ctx = $storageAccount.Context

$containerName | ForEach-Object { Get-AzStorageBlob -Container $_ -Context $ctx }

When i run the last command it throws me the error as shown in the screenshot. Any idea what i am doing wrong and how to fix this

enter image description here


Solution

  • Instead of passing $_ to '-contianer' flag you need to pass $_.Name to container parameter in foreach-object.

    Posting making the above changes to the shared script we are able to pull all the bolbs inside the container.

    Here is the modified script :

    $storageAccountName = "<strgAccountName>"
    $resourceGroup = "<rgName>"
    
    $storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $storageAccountName).Value[0]
    $context=New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountkey
    
    $containerName =   Get-AzStorageContainer -Context $context
    
    $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
    
    $containerName | ForEach-Object { Get-AzStorageBlob -Container $_.Name -Context $context }
    

    Here is the sample output for reference:

    enter image description here