save azure storage account properties to csv

Try to avoid adding elements to an array (which has a static length) using += addition.
By doing that, you will waste time and memory because for every item you add to it, the entire array needs to be rebuilt in memory.

The fix is to let PowerShell collect the data for you like below.

As for the output to Csv, add switch -NoTypeInformation to the Export-Csv cmdlet so it will not write field type information at the top.

$Subscriptions = Get-AzSubscription
$data = foreach ($sub in $Subscriptions) {
    # suppress output on this line
    $null = Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
    Get-AzStorageAccount | ForEach-Object {
        Write-Host Checking $_.StorageAccountName
        $tls = (Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName -Name $_.StorageAccountName).MinimumTlsVersion
        # create and output a custom object to store the information about a storage account 
        [PsCustomObject]@{
            StorageAccountName = $_.StorageAccountName
            ResourceGroupName  = $_.ResourceGroupName
            TLSVersion         = $tls
        }
    }
}

# write a CSV file containing this data
$data | Export-Csv -Path .\data.csv -NoTypeInformation

Note: Export-Csv by default uses the comma as field delimiter character. If you want a different character, you can specify that using the -Delimiter parameter.


In fact, you could write the above without the ForEach-Object loop like this:

$Subscriptions = Get-AzSubscription
$data = foreach ($sub in $Subscriptions) {
    # suppress output on this line
    $null = Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
    # let Select-Object output the objects that will be collected in variable $data
    Get-AzStorageAccount | Select-Object StorageAccountName, ResourceGroupName,
                                         @{Name = 'TLSVersion'; Expression = {$_.MinimumTlsVersion}}

}

# write a CSV file containing this data
$data | Export-Csv -Path .\data.csv -NoTypeInformation

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top