SDB519
SDB519

Reputation: 11

Public Containers in Storage Accounts

First post. My Powershell knowledge isnt great. I am trying to list out all containers that have the "PublicAccess" attribute set to On. I am trying to use the script provided by MS.

$rgName = "<Resource Group name>"
$accountName = "<Storage Account Name>"

$storageAccount = Get-AzStorageAccount -ResourceGroupName $rgName -Name $accountName
$ctx = $storageAccount.Context

Get-AzStorageContainer -Context $ctx | Select Name, PublicAccess

However I need to do this on a large amount of storage accounts. In the past I have used "foreach($item in $list)" to pass things into a small script. But never for multiple lists. Can anyone help?

Upvotes: 1

Views: 143

Answers (1)

Ash
Ash

Reputation: 3266

Based off your extended requirements in the comments, this should work. (I've not tested it and not handled any potential errors related to permissions or anything else)

# Create a collection for the items found.
$StorageAccounts = [System.Collections.Generic.List[System.Object]] @{}

# Loop through the available Azure contexts.
foreach ($Context in (Get-AzContext -ListAvailable)) {
    # Set each subscription in turn, voiding the output.
    [System.Void](Set-AzContext -Context $Context)
    
    # Create an object with the container name, public access values and the name of the storage account/subscription.
    $StorageAccountInfo = Get-AZStorageAccount | Get-AzStorageContainer | Select-Object Name, PublicAccess, @{l = "StorageAccountName"; e = { $_.Context.StorageAccountName } }, @{l = "Subscription"; e = { $Context.Subscription.Name } }

    # If there is data found, add it to the collection.
    if ($null -ne $StorageAccountInfo) {
        $StorageAccounts.AddRange($StorageAccountInfo)
    }
}

# Export the collected information to Csv.
$StorageAccounts | Export-Csv -Path .\myStorageAccounts.csv -NoClobber -Encoding utf8

Upvotes: 1

Related Questions