additional column in excel

Welcome Forums General PowerShell Q&A additional column in excel

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 month, 1 week ago.

  • Author
    Posts
  • #120765

    Participant
    Points: 76
    Rank: Member

    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!

  • #120789

    Participant
    Points: 866
    Helping Hand
    Rank: Major Contributor

    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 
    
  • #120814

    Participant
    Points: 76
    Rank: Member

    thank you kvprasoon! 🙂

You must be logged in to reply to this topic.