Delen via


Containers verwijderen op basis van het containernaamvoorvoegsel

Met dit script worden containers in Azure Blob-opslag verwijderd op basis van een voorvoegsel in de containernaam.

Voor dit voorbeeld Azure PowerShell is vereist. Voer Get-Module -ListAvailable Az uit om de versie te bekijken. Als u PowerShell wilt installeren of upgraden, raadpleegt u De Azure PowerShell-module installeren.

Voer de cmdlet Connect-AzAccount uit om verbinding te maken met Azure.

Als u geen Azure-abonnement hebt, kunt u een gratis Azure-account maken voordat u begint.

Voorbeeldscript

# this script will show how to delete containers with a specific prefix 
# the prefix this will search for is "image". 
# before running this, you need to create a storage account, create some containers,
#    some having the same prefix so you can test this
# note: this retrieves all of the matching containers in one command 
#       if you are going to run this against a storage account with a lot of containers
#       (more than a couple hundred), use continuation tokens to retrieve
#       the list of containers. We will be adding a sample showing that scenario in the future.

# these are for the storage account to be used
#   and the prefix for which to search
$resourceGroup = "containerdeletetestrg"
$storageAccountName = "containerdeletetest"
$prefix = "image"

# get a reference to the storage account and the context
$storageAccount = Get-AzStorageAccount `
  -ResourceGroupName $resourceGroup `
  -Name $storageAccountName
$ctx = $storageAccount.Context 

# list all containers in the storage account 
Write-Host "All containers"
Get-AzStorageContainer -Context $ctx | select Name

# retrieve list of containers to delete
$listOfContainersToDelete = Get-AzStorageContainer -Context $ctx -Prefix $prefix

# write list of containers to be deleted 
Write-Host "Containers to be deleted"
$listOfContainersToDelete | select Name

# delete the containers; this pipes the result of the listing of the containers to delete
#    into the Remove-AzStorageContainer command. It handles all of the containers in the list.
Write-Host "Deleting containers"
$listOfContainersToDelete | Remove-AzStorageContainer -Context $ctx 

# show list of containers not deleted 
Write-Host "All containers not deleted"
Get-AzStorageContainer -Context $ctx | select Name

Opschonen van implementatie

Gebruik de volgende opdracht om de resourcegroep, de resterende containers en alle gerelateerde resources te verwijderen.

Remove-AzResourceGroup -Name containerdeletetestrg

Uitleg van het script

In dit script worden de volgende opdrachten gebruikt containers te verwijderen op basis van het voorvoegsel van de containernaam. Elke opdracht in de tabel is een koppeling naar specifieke documentatie over de opdracht.

Command Aantekeningen
Get-AzStorageAccount Hiermee haalt u een opgegeven opslagaccount of alle opslagaccounts in een resourcegroep of het abonnement op.
Get-AzStorageContainer Hiermee worden de opslagcontainers weergegeven die zijn gekoppeld aan een opslagaccount.
Verwijder-AzStorageContainer Hiermee verwijdert u de opgegeven opslagcontainer.

Volgende stappen

Zie voor meer informatie over de Azure PowerShell-module de documentatie van Azure PowerShell.

Meer PowerShell-voorbeeldscripts voor Storage vindt u in de PowerShell-voorbeelden voor Azure Blob Storage.