Listing Azure Blobs with PowerShell

Image Description

Daily PowerShell #66

Daily PowerShell Azure

January 8, 2022

quote Discuss this Article

In this post, we’ll look at how to list Azure blobs with PowerShell.

Azure Blob Storage

Blob storage allows you to store files within Azure for a low cost per gigabyte. In this post, we’ll be listing files stored in Azure blob storage using the Web API provided by Azure. We will assume that the container Public access level is set to Container. This ensures that non-authenticated requests will list the files.

Azure Blob URL

To list blobs, you will first need to construct the URL for your storage account and container. The URL will have the format: https://{storageAccount}.blob.core.windows.net/{container}.

For example, the imsreleases container is where we store our production and nightly builds of Ironman Software products. One of the containers, universal, contains all the releases of our production PowerShell Universal builds. The URL for this container is:

https://imsreleases.blob.core.windows.net/universal

Accessing the Blob from PowerShell

You can access the blob from PowerShell using Invoke-RestMethod. You will need to include the restype and comp query string parameters to return XML string.

Invoke-RestMethod "https://imsreleases.blob.core.windows.net/universal?restype=container&comp=list"

Processing the Blob XML

Once you have accessed the URL, you will need to convert the XML string to objects. You can cast to an [xml] type to do so. You will need to trim off the first three characters.

[xml]$xml = (Invoke-RestMethod "https://imsreleases.blob.core.windows.net/universal?restype=container&comp=list").Substring(3)

Once you have your XML, you can access the blobs within the object.

$xml.EnumerationResults.Blobs.Blob

<#
Name                                                    Url
----                                                    ---
83/PowerShellUniversal.1.0.1.msi                        https://imsreleases.blob.core.windows.net/universal/83/PowerShellUniversal.1.0.1.msi
83/Universal.1.0.1.zip                                  https://imsreleases.blob.core.windows.net/universal/83/Universal.1.0.1.zip
logo.png                                                https://imsreleases.blob.core.windows.net/universal/logo.png
production/1.0.0/PowerShellUniversal.1.0.0.msi          https://imsreleases.blob.core.windows.net/universal/production/1.0.0/PowerShellUniversal.1.0.0.msi
#>

Access Blob Properties

Each blob will have a name, URL and properties. Properties include information such as when the file was last modified and content type.

$xml.EnumerationResults.Blobs.Blob[0].Properties

<#
Last-Modified    : Tue, 05 May 2020 00:05:46 GMT
Etag             : 0x8D7F0880F8A800C
Content-Length   : 106254336
Content-Type     : application/octet-stream
Content-Encoding :
Content-Language :
Content-MD5      :
Cache-Control    :
BlobType         : BlockBlob
LeaseStatus      : unlocked
#>