使用 Azure CLI 在訂用帳戶中尋找現有的 Azure Cosmos DB 免費層帳戶

適用於:NoSQL MongoDB Cassandra Gremlin Table

本文中的指令碼示範如何在訂用帳戶內找出 Azure Cosmos DB 免費層帳戶。

每個 Azure 訂用帳戶最多可以有一個 Azure Cosmos DB 免費層帳戶。 如果您嘗試建立免費層帳戶,Azure 入口網站中可能會停用該選項,或者,您可能會在嘗試建立免費層帳戶時收到錯誤。 如果發生上述任一問題,請使用此指令碼來找出現有免費層帳戶的名稱,及其所屬的資源群組。

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

  • 本文需要 2.9.1 版或更新版本的 Azure CLI。 如果您是使用 Azure Cloud Shell,就已安裝最新版本。

範例指令碼

啟動 Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。

若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。 您也可以移至 https://shell.azure.com ,從另一個瀏覽器索引標籤啟動 Cloud Shell。

當開啟 Cloud Shell 時,請確認已為您的環境選取 Bash。 後續的工作階段將會在 Bash 環境中使用 Azure CLI,請選取 [複製] 以複製程式碼區塊,並將其貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。

登入 Azure

系統會在登入的初始帳戶下自動驗證 Cloud Shell。 使用下列指令碼透過不同的訂閱登入,並將 <Subscription ID> 取代為您的 Azure 訂用帳戶識別碼。 如果您沒有 Azure 訂用帳戶,請在開始之前先建立 Azure 免費帳戶

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

如需詳細資訊,請參閱設定使用中訂閱以互動方式登入

執行指令碼

# Azure Cosmos DB offers one free-tier account per subscription
# This script will find if you have a free-tier account and output 
# the name of the Cosmos DB account and its resource group 


# These can remain commented out if running in Azure Cloud Shell

#az login
#az account set -s {your subscription id}

isFound=0

# Iterate through all the resource groups in the subscription
for rg in $(az group list --query "[].name" --output tsv) 
do

    echo "Checking resource group: $rg"
    
    # Return the Cosmos DB account in the resource group marked as free tier
    ft=$(az cosmosdb list -g $rg --query "[?enableFreeTier].name" --output tsv)
    
    if [ ${#ft} -gt 0 ]; then
        
        echo "$ft is a free tier account in resource group: $rg"
        isFound=1
        break
    
    fi

done

if [ $isFound -eq 0 ]; then
    echo "No Free Tier accounts in subscription"
fi

範例參考

此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。

Command 注意
az group list 列出 Azure 訂用帳戶中的所有資源群組。
az cosmosdb list 列出資源群組中的所有 Azure Cosmos DB 帳戶。

下一步

如需 Azure Cosmos DB CLI 的詳細資訊,請參閱 Azure Cosmos DB CLI 文件

如需特定 API 的 Azure CLI 範例,請參閱: