Get-AzStorageBlob: Could not get the storage context

In this Azure PowerShell article, we will discuss how to fix the error “Could not get the storage context. Please pass in a storage context or set the current storage context.” which I came across while executing the Get-AzStorageBlob PowerShell command.

Get-AzStorageBlob: Could not get the storage context

Recently, while working on one of the requirements where I was trying to execute the Get-AzStorageBlob PowerShell command. I was executing the below Azure PowerShell command.

Get-AzStorageBlob -Container "azure-webjobs-hosts" -IncludeVersion

The moment I ran this command, I got this error. The complete error message was as below.

Get-AzStorageBlob : Could not get the storage context. Please pass in a storage
context or set the current storage context.
At line:2 char:1

Get-AzStorageBlob -Container "azure-webjobs-hosts" -IncludeVersion

~~~~~~~~~~~~~~

CategoryInfo : CloseError: (:) [Get-AzStorageBlob], InvalidOperationEx
ception

FullyQualifiedErrorId : InvalidOperationException,Microsoft.WindowsAzure.Comman
ds.Storage.Blob.Cmdlet.GetAzureStorageBlobCommand


You can see the same output below

Get-AzStorageBlob: Could not get the storage context

Get-AzStorageBlob: Could not get the storage context [Solved]

So to fix this error, I had to pass in a storage context. So I ran the below PowerShell script.

$sARG = "DEMORG2"
$sAName = "demorg2913f"
$sAKey = (Get-AzStorageAccountKey -ResourceGroupName $sARG -AccountName $sAName).Value[0]
$myctx = New-AzStorageContext -StorageAccountName $sAName -StorageAccountKey $sAKey
Get-AzStorageBlob -Container "azure-webjobs-hosts" -Context $myctx

After executing the above PowerShell script, I got the below-expected output.

AccountName: demorg2913f, ContainerName: azure-webjobs-hosts

Name                 BlobType  Length          ContentType                    LastModi
                                                                              fied    
----                 --------  ------          -----------                    --------
locks/demotstest/... BlockBlob 0               application/octet-stream       2023-...

You can see the same output below

Could not get the storage context

You may also like following the below articles

Wrapping Up

In this Azure PowerShell article, we discussed how to fix the error “Could not get the storage context. Please pass in a storage context or set the current storage context.” which I got while executing the Get-AzStorageBlob PowerShell command. Thanks for reading this article !!!