1

I am trying to fetch the list of blobs present in the Azure Blob Container from PowerShell.

I have tried using function in my script to do that. But it is returning nothing.

My script is somewhat like this(Hiding names of resources):

## Connect to Azure Account  
Connect-AzAccount   

Function GetBlobs  
{  
    ## Get the storage account   
    $storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName     

    ## Get all the containers  
    $containers=Get-AzStorageContainer  

    ## Get all the blobs  
    $blobs=Get-AzStorageBlob -Container $containerName 

    ## Loop through all the blobs  
    foreach($blob in $blobs)  
    {  
        write-host $blob.Name  
    }  
}  
  
GetBlobs   

But it returned blank though I have blobs in my container. I don't know what I'm doing wrong.

Can someone help me out? I'm new to this platform too, don't know if I put my question in the right way.

2
  • 2
    That doesn't look complete. Your getting a list of all containers using Get-AzStorageContainer. In the next step you use the uninitialized variable $containerName to access a single one. Did you forget an (outer) loop over all containers? Commented Mar 14, 2022 at 10:19
  • I'm giving the name of my container in $containerName. Commented Mar 14, 2022 at 11:06

1 Answer 1

2

I have tested in my environment. It returned the list of blobs successfully.

1

Try including storage account context. If you want to know more about this, go through this link. After including that, you may get the list of blobs successfully.

Please check if your script is something like this:

$resourceGroupName = "your_RG"
$storageAccountName= "your_SA"
$containerName= "your_container_name"
 
## Connect to Azure Account  
Connect-AzAccount   
 
## Function to get all the blobs  
Function GetBlobs  
{  
    $storageAcc=Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName     
    ## Get the storage account context  
    $context=$storageAcc.Context  
    ## Get all the containers  
    $containers=Get-AzStorageContainer -Context $context      
    $blobs=Get-AzStorageBlob -Container $containerName -Context $context
    foreach ($blob in $blobs)  
    {  
        write-host $blob.Name  
    }  
}  
  
GetBlobs   

Please check the below reference if it is helpful.

Reference:

How to Get All the Blobs from an Azure Storage Account using PowerShell (c-sharpcorner.com)

Sign up to request clarification or add additional context in comments.

Comments

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.