From b6a3db77059cc4387f31788178795565155294a3 Mon Sep 17 00:00:00 2001 From: Pengcheng Tang Date: Tue, 14 Sep 2021 18:10:29 -0700 Subject: [PATCH] Add sample powershell script to list storage blobs --- blog/tip333.md | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 blog/tip333.md diff --git a/blog/tip333.md b/blog/tip333.md new file mode 100644 index 0000000000..773f2597e8 --- /dev/null +++ b/blog/tip333.md @@ -0,0 +1,55 @@ +--- +type: post +title: "Tip 333 - How to list all blobs with types and access tiers" +excerpt: "How to list all blobs in a storage account" +tags: [Storage] +share: true +date: 2021-9-14 05:00:00 +--- + +### Prerequisites +If you want to follow along, you'll need the following: +* An Azure subscription (If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=azure-azuredevtips-azureappsdev) before you begin) +* Create an [Azure Storage Account](https://docs.microsoft.com/azure/storage/common/storage-account-create?tabs=azure-portal) +* Install [Az PowerShell modules](https://docs.microsoft.com/powershell/azure/install-az-ps) if not already +* Use an authenticated account or service principal to connect to the storage account + +### Copy and execute below Powershell script to list all blobs in a storage account +The time it takes to enumerate all blob objects will depend on the volume of your storage account. Output will be saved in a file named "blobs.txt" under the path you specify. + +#### Provide inputs by following the instructions +``` +$subID = Read-Host "`n Enter your Subscription ID: " +$rg = Read-Host "`n Enter the name of your Resource Group: " +$sa = Read-Host "`n Enter the name of your Storage Account: " +$path = Read-Host "`n Enter the path where you want to keep the generated report (blobs.txt): " +$strAccountName = "$sa" +$strAccountRG = "$rg" +# Connect to Az Account +Connect-AzAccount +# Choose subscription +Select-AzSubscription -SubscriptionId "$subID" +# create context +$stCtx = New-AzStorageContext -StorageAccountName $strAccountName -StorageAccountKey ((Get-AzStorageAccountKey -ResourceGroupName $strAccountRG -Name $strAccountName).Value[0]) +# fetch containers +$containers = Get-AzStorageContainer -Context $stCtx +# placeholder to hold file list +$array = @(); +# Property names to display +$array += -join("Container", "; ", "Blob", "; ", "BlobType", "; ", "AccessTier") +$array += "---------------------------------------------------------------------" +foreach($container in $containers) +{ + # fetch blobs in current container + $blobs = Get-AzStorageBlob -Container $container.Name -Context $stCtx + + # list all blobs and fetch properties + foreach($blob in $blobs) + { + $array += -join($container.Name, "; ", $blob.Name, "; ", $blob.BlobType, "; ", $blob.BlobProperties.AccessTier) + } +} +# Export to file + +$array | Out-File -FilePath "$path\blobs.txt" -NoClobber +```