Home > Blockchain >  Save Azure Storage Account properties to CSV
Save Azure Storage Account properties to CSV

Time:01-04

I'm running following piece of code to get Storage Account TLS version from all my Storage Accounts from all my subscriptions. Only issue at the moment is that I can't export result to CSV so I could forward results. What to add to code so that it would save Storage account name, Resouce group, subscription and TSL version to one row but in separate columms?

  [PsObject[]]$data = @()

#$Subscriptions = Get-AzSubscription
#foreach ($sub in $Subscriptions) {
    #Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
    $storageaccounts = Get-AzStorageAccount
    $storageaccounts| ForEach-Object {
      Write-Host Checking $_.StorageAccountName
      $tls = (Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName -Name $_.StorageAccountName).MinimumTlsVersion
      # create a custom object to store the information about a storage account 
      $tlsVersionData = [PsObject]@{
        "StorageAccountName" = $_.StorageAccountName
        "ResourceGroupName" = $_.ResourceGroupName
        "TLSVersion" = $tls
      }
      # add that custom object to the array
      $data  = $tlsVersionData
    }
#}

# write a CSV file containing this data
$data | Export-Csv C:\temp\data.csv
  

Write host shows right information about storage account to screen but I'm bit lost how to save data to to cvs.

CodePudding user response:

Please try something like below:

[PsObject[]]$data = @() #define an array that will hold the data

$Subscriptions = Get-AzSubscription
foreach ($sub in $Subscriptions) {
    Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
    $storageaccounts = Get-AzStorageAccount
    $storageaccounts| ForEach-Object {
      Write-Host Checking $_.StorageAccountName
      $tls = (Get-AzStorageAccount -ResourceGroupName $_.ResourceGroupName -Name $_.StorageAccountName).MinimumTlsVersion
      # create a custom object to store the information about a storage account 
      $tlsVersionData = [PsObject]@{
        "StorageAccountName" = $_.StorageAccountName
        "ResourceGroupName" = $_.ResourceGroupName
        "TLSVersion" = $tls
      }
      # add that custom object to the array
      $data  = $tlsVersionData
    }
}

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

CodePudding user response:

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
  • Related