摘要
在本課程模組中,您建立了 Azure CLI 指令碼來自動建立多個 VM。 儘管它相對簡單,但該指令碼展示了將迴圈和變數與 Azure CLI 參考命令結合時 Azure CLI 的強大潛力。
Azure CLI 是自動管理 Azure 資源的絕佳選擇。 其簡潔的語法和強大的指令碼功能讓它成為一個有價值的工具,即使對於剛接觸 Bash 的系統管理員也是如此。 透過自動化耗時且容易出錯的工作,您可以減少管理開銷並提高整體效率和品質。
清理
當您完成此課程模組時,沙箱會自動清除您的資源。
如果您是在自己的訂用帳戶中進行,建議您在專案結束時判斷自己是否仍需要先前所建立的資源。 若您繼續執行資源,則可能會產生費用。 您可以個別刪除資源,或刪除資源群組以刪除整組資源。
刪除資源群組
警告
下列範例會刪除資源群組及其內含的所有資源。 如果指定的資源群組中存在此訓練模組範圍以外的資源,則它們也會被刪除。
使用 az group delete
命令來刪除單一資源群組及所有相關資源。
az group delete --name myResourceGroupName
當系統提示確認刪除時,請回答 Y 並按 Enter,或新增 --yes
參數以跳過提示。 命令可能需要幾分鐘的時間才能完成。
使用指令碼刪除資源群組
測試時一次刪除一個資源群組可能會很繁瑣。 如果您在測試過程中使用了命名慣例,請考慮使用指令碼。 此範例會刪除所有名稱以 msdocs 開頭的資源群組。
#!/bin/bash
# Set your subscription if you haven't already
subscriptionID=00000000-0000-0000-0000-00000000
az account set --subscription $subscriptionID
# 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)
for rgList in $(az group list --query "[?starts_with(name, 'msdocs') == \`true\`].name" -o tsv);
do
echo "deleting resource group $rgList"
az group delete --name $rgList --yes --no-wait
done
# get the status of all resource groups in the subscription
az group list --output table