List all azure-blobs with metadata into a textfile (like "dir > listmyfolder. txt" in cmd )

693 views Asked by At

I need to get a list off all blobs we have in a azure storage account. We have about 10'000 containers and 7'000'000 blobs.

I'm using PowerShell, but I'm a noob. Is there a way to get this information into a text file? I need the name and container of each blob. If I can get the size and created date it would be nice.

1

There are 1 answers

6
PowerShellGuy On

It depends on your version of PowerShell $PSVersionTable.PSVersion

If you're on or above 5.1 and above, you can use The Az module

Then I assume this cmdlet and/or this one with wildcards.

Normally I'd walk you through the exact steps, but I'm an AWS guy, and my azure experience is very limited. With that in mind, I think it would look something like this

You'd get everything you need (assuming I'm correct) and store it in a variable

$allTheThings = Get-AzStorageContainer -Name * | Get-AzStorageBlob

Then you can explore the objects, and figure out what information you need out of them, by grabbing the first one in the array, and listing all of its properties

# You can do this, which will just show the properties of the object
$allTheThings[0] | Get-Member

# Or this, which will show the values of each of the properties
$allTheThings[0] | Format-List *

Once you figure out what information you'd like to extract, you can create a custom class, then use a foreach loop to build an array full of those custom objects, and export it to a csv doc.

class MySuperAwesomeClass 
{
    $PropertyIWant1
    $PropertyIWant2
    $PropertyIWant3
    $PropertyIWant4
    $PropertyIWant5
    $PropertyIWant6
    $PropertyIWant7

}

$allTheThings = Get-AzStorageContainer -Name * | Get-AzStorageBlob

$listofInfoIWant = @()

foreach($thing in $allTheThings)
{
    $myCustomObj = New-Object -TypeName MySuperAwesomeClass

    $myCustomObj.PropertyIWant1 = $thing.property1
    $myCustomObj.PropertyIWant2 = $thing.property2
    $myCustomObj.PropertyIWant3 = $thing.property3
    $myCustomObj.PropertyIWant4 = $thing.property4
    $myCustomObj.PropertyIWant5 = $thing.property5
    $myCustomObj.PropertyIWant6 = $thing.property6
    $myCustomObj.PropertyIWant7 = $thing.property7

    $listofInfoIWant += $myCustomObj
}

$listofInfoIWant | Export-Csv -Path C:\temp\azStorageInfo.csv -NoTypeInformation

# this will auto-open the newly created csv doc, comment it out to not do that
ii C:\temp\azStorageInfo.csv