Working script to search a whole blob container based on user input

Rik Hunter 1 Reputation point
2021-03-06T18:36:12.93+00:00

Hi,

I have recently joined the world of DevOps and I'm pretty much used to working with other powershell stuff that I have not made from scratch but I have a pain point with Azure storage explorer.

The search (filter) only allows you to search the current folder.

Now, I know it's possible to list contents in powershell and even search for a string but I believe my code is very basic (yes, and shoddy).

Is anyone able to point me in the right direction?
Thanks.

VARIABLES#######

ENTER SOME STUFF #

$containerName = Read-host -prompt 'Enter Container Name'
$filename = Read-host -prompt 'Enter Phone Number'
$ctx = CONTEXT GOES HERE?

FUNCTION#######

SCRIPT DO SOME STUFF #

Get-AzStorageBlob -Context $ctx -Container $containerName | Where-Object {$_.Name -like $filename }

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,415 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. shiva patpi 13,131 Reputation points Microsoft Employee
    2021-03-07T02:04:33.183+00:00

    Hello @Rik Hunter ,
    Can you try out the below sample script . While running pass the storage account name and the blob name to be searched.
    Script will loop through all the containers in the storage account and search for the specific blob name passes as a paramter.

    FYI - Script can be optimized much better instead of using embedded for loops. Right now the time complexity of the below script is O(n^2). I am sure this can be done in O(n).

    Below module will search for a particular blob in all the containers of a specific storage account

    param(
    [Parameter(Mandatory)]$strAccountName,
    [Parameter(Mandatory)]$BlobName
    )
    Function RecursiveSearchForABlobInAllContainers()
    {

    enter the subscriptionid

    **#login to Azure Account**  
    **#Try to set the subscription context here or manually in the command prompt**  
    # Set-AzContext "SubID"  
    

    #Get all Azure storage accounts under the subscription
    $AllStorageAccounts = Get-AzStorageAccount
    #Loop through each storage account
    Write-Host "Looping through all the Storage Accounts under the subscription"
    foreach($stAcc in $AllStorageAccounts)
    {

    if ($stAcc.StorageAccountName -eq $strAccountName)
    {
    Write-Host "Reading Storage Account:" $stAcc.StorageAccountName -ForegroundColor "Yellow"
    #Get the Storage account details under the resource group
    try
    {
    $strAccount = Get-AzStorageAccount -ResourceGroupName $stAcc.ResourceGroupName -Name $stAcc.StorageAccountName
    }
    catch
    {
    Write-Host "Below error occured while accessing storage Account :- " $stAcc -ForegroundColor "Yellow"
    Write-Host $Error
    }
    #Get the context
    $context = $strAccount.Context
    #Get the Container
    try
    {
    $Containers = Get-AzStorageContainer -Context $context
    }
    catch
    {
    Write-Host "Below error occured while accessing storage Account :- " $stAcc -ForegroundColor "Yellow"
    Write-Host $Error
    }
    #Loop through all the containers
    foreach($container in $containers)
    {
    #Get all the blobs in the container
    $blobData = Get-AzStorageblob -Container $container.Name -Context $context
    #Loop through the blobs
    foreach($blob in $blobData)
    {
    if ($blob.Name -match $BlobName) #it will match for a substring
    {
    Write-Host "Blob: " $BlobName " found under path: " $blob.Name -ForegroundColor Green
    }
    }
    }
    }
    else
    {
    Write-Host "Storage Account:" $strAccountName " does not exist" -ForegroundColor Red
    }
    }
    }

    RecursiveSearchForABlobInAllContainers

    Let us know if the above script helps you out to get started. If it is useful , Please make sure to "Upvote and Accept the Answer"

    0 comments No comments