Megosztás a következőn keresztül:


Erőforrások nagy léptékű törlése az Azure CLI használatával

Azure-erőforrás-kezelőként gyakran több Azure-erőforrást is törölnie kell egy régi környezet lebontásakor. Egyes CLI devTest-környezetek rendszeres tisztítást is igényelnek, így nem merülnek fel díjak a hosszabb ideig eltartott ideiglenes Azure-erőforrásokért.

Ebben az Azure CLI-példában a következőket fogja elsajátítani:

  • Több Azure-erőforrás törlése szkriptből
  • Naplószkript állapota helyi TXT-fájlba

Ezt a mintaszkriptet bash-környezetben teszteltük az Azure Cloud Shellben . Ezt a szkriptet az Ubuntu 22.04.3 LTS-ben is sikeresen teszteltük a Windows Terminal használatával.

Azure-erőforrások név szerinti szűrésének törlése

Ezzel a szkripttel listázhatja és törölheti azokat az erőforráscsoportokat, amelyek egy adott szóból indulnak ki.

# Set your subscription
subscriptionID=00000000-0000-0000-0000-00000000
az account set --subscription $subscriptionID

# Set your log file location
logFileLocation="myLogName.txt"

# Get the name of all resource groups that start with 'msdocs'
az group list --query "[?starts_with(name, 'msdocs') == \`true\`].name" -o table

# Delete resource groups without a confirmation prompt (--yes)
# Do not wait for the operation to finish (--no-wait)
echo "Deleting resource groups">$logFileLocation
for rgList in $(az group list --query "[?starts_with(name, 'msdocs') == \`true\`].name" -o tsv); 
do
    echo "deleting resource group $rgList">>$logFileLocation
    az group delete --name $rgList --yes --no-wait
done

# read your log file with Linux "cat" command
clear
cat $logFileLocation

Azure-erőforrások szűrésének törlése létrehozási dátum szerint

Ezzel a szkripttel listázhatja és törölheti a dátumtartományon belül létrehozott tárfiókokat.

# Set your log file location
logFileLocation="myLogName.txt"

# Set your resource group variable
rgName=<msdocs-rg-0000000>

# Get a list of Azure storage accounts that were created in the last 30 days. Return the results as a table.
saDate=$(date +%F -d "-30days")
az storage account list --resource-group $rgName \
                        --query "[?creationTime >='$saDate'].{saName:name, createdTimeStamp:creationTime}" \
                        --output table

# Delete storage accounts without a confirmation prompt (--yes).
# Do not wait for the operation to finish (--no-wait)
echo "Deleting storage accounts">$logFileLocation
for saList in $(az storage account list --resource-group $rgName \
                        --query "[?creationTime >='$saDate'].{saName:name, createdTimeStamp:creationTime}" \
                        --output tsv);
do
    echo "deleting storage account $saList">>$logFileLocation
    az storage account delete --ids $saList --yes --no-wait
done

# read your log file with Linux "cat" command
clear
cat $logFileLocation

Az összes típusú Azure-erőforrás törlése

Erőforráscsoport összes virtuális gépének törlése

# Set your resource group variable
rgName=<msdocs-rg-0000000>

az group delete -n $rgName --force-deletion-types Microsoft.Compute/virtualMachines

Lásd még