Last active
January 31, 2024 02:12
-
-
Save marckean/1fcfe48bbe1198c3e2c5c16733ce72a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Like an inventory of all blobs across all storage accounts | |
$Subscription_IDs = @("ID_One-hf49h4h98h", "ID_Two-8f4jfj2409") | |
foreach ($Subscription_ID in $Subscription_IDs) { | |
Get-AzSubscription -SubscriptionId $Subscription_ID | Select-AzSubscription | |
# Retrieve all storage accounts | |
$storageAccounts = Get-AzStorageAccount | where { !($_.defaultToOAuthAuthentication) -and $_.AllowBlobPublicAccess -eq $true -and $_.StorageAccountName -notmatch 'cs.*' -and $_.StorageAccountName -ne 'dtaawsarchive'} | |
foreach ($storageAccount in $storageAccounts) { | |
Write-Host -ForegroundColor Cyan "Current storage account " -NoNewline;Write-Host -ForegroundColor Magenta "$($storageAccount.StorageAccountName)" | |
$resourceGroupName = $storageAccount.ResourceGroupName | |
$storageAccountName = $storageAccount.StorageAccountName | |
# Get the storage account context | |
$context = $storageAccount.Context | |
# Retrieve all containers in the storage account and filter based on PublicAccess and Name | |
$containers = Get-AzStorageContainer -Context $context | Where-Object { $_.Name -notlike '$*' } | |
# Proceed only if there are eligible containers | |
if ($containers.Count -gt 0) { | |
# Initialize an empty array to hold the blob details for this storage account | |
$blobDetails = @() | |
foreach ($container in $containers) { | |
Write-Host -ForegroundColor Green "Current storage account container " -NoNewline;Write-Host -ForegroundColor Yellow "$($container.Name)" | |
# Retrieve all blobs in the container | |
$blobCount = 0 | |
$Token = $Null | |
$MaxReturn = 5000 | |
do { | |
$Blobs = Get-AzStorageBlob -Context $context -Container $container.Name -Prefix $folderName -MaxCount $MaxReturn -ContinuationToken $Token | |
Write-Host -ForegroundColor Magenta "Current token is " -NoNewline;Write-Host -ForegroundColor Blue "$($Token.NextMarker)" | |
if ($Blobs -eq $Null) { break } | |
#Set-StrictMode will cause Get-AzureStorageBlob returns result in different data types when there is only one blob | |
if ($Blobs.GetType().Name -eq "AzureStorageBlob") { | |
$Token = $Null | |
} | |
else { | |
$Token = $Blobs[$Blobs.Count - 1].ContinuationToken; | |
} | |
foreach ($blob in $blobs) { | |
if($blob.BlobType -eq "BlockBlob") { | |
# Add blob details to the array | |
$blobDetails += New-Object PSObject -Property @{ | |
StorageAccount = $storageAccountName | |
ResourceGroup = $resourceGroupName | |
Container = $container.Name | |
Blob = $blob.Name | |
LastModified = $blob.ICloudBlob.Properties.LastModified.DateTime | |
} | |
} | |
} | |
} | |
While ($Token -ne $Null) | |
} | |
# Define file name for this storage account | |
$file = "$env:TEMP\AzureBlobDetails_${storageAccountName}.csv" | |
# Export the details to a CSV file | |
$blobDetails | Export-Csv -Path $file -NoTypeInformation | |
# Optional: Output to indicate progress | |
Write-Host "File saved for storage account $storageAccountName_$file" | |
} | |
} | |
} | |
# Confirmation message | |
Write-Host "Export completed for all storage accounts." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment