additional column in excel

Dear All,

 

I’m trying to pull mulitple Azure storage accounts and there containers information and put into CSV,

Get-AzureRmStorageAccount |
ForEach-Object -Process {

$resourceGroupName = $_.ResourceGroupName
$storageAccountName = $_.StorageAccountName
$storageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[0]
$context = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

Get-AzureStorageContainer -Context $context | Get-AzureStorageBlob | Export-Csv -Path c:\stg.csv 
}

gives the information I want, but it doesn’t tell this blob belongs to this storage account, it simply pulls all the blobs from the containers of the storage and puts into csv, how do I add an additional column which says this blob belongs to this storage account ?

Thank you for you help!

Here comes beauty of calculated properties,

Get-AzureRmStorageAccount |
ForEach-Object -Process {

$resourceGroupName = $_.ResourceGroupName
$storageAccountName = $_.StorageAccountName
$storageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[0]
$context = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

Get-AzureStorageContainer -Context $context | Get-AzureStorageBlob | Select-Object -Property *, @{E={$storageAccountName};L='StorageAccountName'}| Export-Csv -Path c:\stg.csv 
}

but with above code, csv file will have only last storage account details. hence you can take the output of the foreach iteration to a variable then export to csv only once.

$CsvDetails = Get-AzureRmStorageAccount | ForEach-Object -Process {

$resourceGroupName = $_.ResourceGroupName
$storageAccountName = $_.StorageAccountName
$storageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[0]
$context = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

Get-AzureStorageContainer -Context $context | Get-AzureStorageBlob | Select-Object -Property *, @{E={$storageAccountName};L='StorageAccountName'}
}

$CsvDetails | Export-Csv -Path c:\stg.csv 

thank you kvprasoon! :slight_smile: