How to get list of Azure container files?

1.8k views Asked by At

I'm working on PS script to list all Storage Accounts, which contains files with a modified date less than < X.

I'm able to list all SA containers, it's not a big deal but I'm not sure how to get further and list all files inside a particular container.

$storageAccCtx = (Get-AzStorageAccount -Name acc_name -ResourceGroupName acc_rg).Context

Get-AzStorageContainer -Context $storageAccCtx

I couldn't find any cmdlet for this.

Could anyone, please, advise what should I use next? Thanks.

2

There are 2 answers

3
Nick Graham On

You can use Get-AzStorageBlob to list the blobs in a storage container, the cmdlet is documented here. In a script you could use this cmdlet as follows to return all the blobs older than a particular date:

$CutOffDate = Get-Date -Year 2020 -Month 10 -Day 19
$OldBlobs = @()
$StorageAccounts = Get-AzStorageAccount
foreach ($StorageAccount in $StorageAccounts) {
    $Containers = Get-AzStorageContainer -Context $StorageAccount.Context
    foreach ($Container in $Containers) {
        $ContainerBlobs = Get-AzStorageBlob -Container $Container.Name -Context $StorageAccount.Context
        $OldBlobs += $ContainerBlobs | Where-Object { $_.LastModified -lt $CutOffDate }
    }
}
$OldBlobs
1
Sanmoy On

Once you have the StorageContext, you can use this below Azure Storage Management Cmdlet to list all BlockBlob.

Get-AzStorageBlob -Container containerName -Context $storageAccCtx

To get list of all Azure Storage Management Cmdlet, please follow this documentation https://learn.microsoft.com/en-us/powershell/module/az.storage/?view=azps-4.8.0