I am trying to list all the blobs from various containers that i have in an Azure storage account using powershell So i run the below commands :
$storageAccountName = "contoso"
$resourceGroup = "Contoso-Rg"
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $storageAccountName).Value[0]
$context=New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountkey
$containerName = Get-AzStorageContainer -Context $context
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$ctx = $storageAccount.Context
$containerName | ForEach-Object { Get-AzStorageBlob -Container $_ -Context $ctx }
When i run the last command it throws me the error as shown in the screenshot. Any idea what i am doing wrong and how to fix this
CodePudding user response:
Instead of passing $_
to '-contianer' flag you need to pass $_.Name
to container parameter in foreach-object.
Posting making the above changes to the shared script we are able to pull all the bolbs inside the container.
Here is the modified script :
$storageAccountName = "<strgAccountName>"
$resourceGroup = "<rgName>"
$storageAccountKey = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $storageAccountName).Value[0]
$context=New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountkey
$containerName = Get-AzStorageContainer -Context $context
$storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$containerName | ForEach-Object { Get-AzStorageBlob -Container $_.Name -Context $context }
Here is the sample output for reference: