Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Add sample powershell script to list storage blobs #152

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions blog/tip333.md
Original file line number Diff line number Diff line change
@@ -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
```