I need to create a program in Powershell where i access my Azure environment and inside a Storage account erase all the blob containers that have a GUID pattern, it needs to list out all containers that will be deleted and the number of containers BEFORE deletion then asks "Are you sure?" or requests confirmation from the user.
I have this code but it still the line for GUID pattern and the Display list to be deleted seem not to work
# Replace the placeholders with the appropriate values for your environment
$storageAccountName = "your-storage-account-name"
$resourceGroupName = "your-resource-group-name"
# Get the list of containers in the storage account
$containers = az storage container list --account-name $storageAccountName --resource-group $resourceGroupName --query "[].{Name: name}" --output table
# Filter the list of containers to only include those with a GUID pattern
$containersToDelete = $containers | Where-Object { $_.Name -match '^[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}$' }
# Display the list of containers to be deleted and the total count
Write-Host "Containers to be deleted:"
$containersToDelete | ForEach-Object { Write-Host " $($_.Name)" }
Write-Host "Total count: $($containersToDelete.Count)"
# Prompt the user for confirmation before deleting the containers
$confirmDelete = Read-Host "Are you sure you want to delete these containers? (Y/N)"
if ($confirmDelete.ToUpper() -eq "Y") {
# Loop through the selected containers and delete them
foreach ($container in $containersToDelete) {
az storage container delete --account-name $storageAccountName --resource-group $resourceGroupName --name $container.Name --yes
}
Write-Host "Deleted $($containersToDelete.Count) containers."
} else {
Write-Host "Operation cancelled."
}
Could you please help me make it work
Thanks!
I tried in my environment and got the below results:
The code you provided seems to be a mix of PowerShell and Azure CLI commands.
First I created a container with GUID using the below commands:
Command:
$containerName = [guid]::NewGuid().ToString() # generate a new GUID and convert to string
$resourceGroupName = "resourceGroupName"
$storageAccountName = "storageAccountName"
$storageAccountkey="storageAccountkey"
$ctx=New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountkey
New-AzStorageContainer -Name $containerName -Context $ctx
Output:
Storage Account Name: "Your storage account name"
Name PublicAccess LastModified IsDeleted VersionId
---- ------------ ------------ --------- ---------
0ae69b48-29aa-4aff-… Off 3/25/2023 5:27:58 AM +00:00
Portal:
I created three GUID containers in the Blob storage.
Now, You can use the modified PowerShell script to delete the GUID containers
.
Command:
$resourceGroupName = "resourceGroupName"
$storageAccountName = "storageAccountName"
$storageAccountkey="storageAccountkey"
$ctx=New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountkey
# Get the list of containers in the storage account
$containers = az storage container list --account-name $storageAccountName --account-key $storageAccountkey --query "[].{Name: name}" --output json | ConvertFrom-Json
# Filter the list of containers to only include those with a GUID pattern
$containersToDelete = $containers | Where-Object { $_.Name -match '^[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}$' }
# Display the list of containers to be deleted and the total count
Write-Host "Containers to be deleted:"
$containersToDelete | ForEach-Object { Write-Host " $($_.Name)" }
Write-Host "Total count: $($containersToDelete.Count)"
# Prompt the user for confirmation before deleting the containers
$confirmDelete = Read-Host "Are you sure you want to delete these containers? (Y/N)"
if ($confirmDelete.ToUpper() -eq "Y") {
# Loop through the selected containers and delete them
foreach ($container in $containersToDelete) {
az storage container delete --account-name $storageAccountName --account-key $storageAccountkey --name $container.Name
}
Write-Host "Deleted $($containersToDelete.Count) containers."
} else {
Write-Host "Operation cancelled."
}
Output:
{
"deleted": true
}
{
"deleted": true
}
{
"deleted": true
}
Deleted 3 containers.