如何使用 Azure CLI 管理 Azure 訂用帳戶

Azure CLI 可協助您管理 Azure 訂用帳戶、建立管理群組,以及鎖定訂用帳戶。您可能在 Azure 內有多個訂用帳戶。 您可以是多個組織的一部分,或者您的組織可能會跨群組分割特定資源的存取權。 Azure CLI 支援全域和每個命令選取訂用帳戶。

如需訂用帳戶、計費和成本管理的詳細資訊,請參閱 計費和成本管理檔

詞彙

租使用者是 Microsoft Entra 識別符的實例,其中有單一組織的相關信息。 多租用戶組織是具有多個 Microsoft Entra 識別符實例的組織。 租使用者有一或多個 訂用帳戶使用者

使用者是登入 Azure 以建立、管理及使用資源的帳戶。 用戶可以存取多個 租用戶訂用帳戶

用帳戶是 Microsoft 使用雲端服務的合約,包括 Azure。 每個資源都會與訂用帳戶相關聯。 訂用帳戶包含資源群組。

Azure 資源群組 是保留 Azure 解決方案相關資源的容器。 若要瞭解如何管理訂用帳戶內的資源群組,請參閱 如何使用 Azure CLI 管理 Azure 資源群組

取得作用中租使用者

使用 az account tenant listaz account show 來取得作用中的租用戶標識符。

az account tenant list

az account show

變更作用中租使用者

若要切換租使用者,您有兩個選項。

  • 變更使用中的訂用帳戶。

  • 以所需租使用者內的使用者身分登入。 使用 az login 來變更作用中的租使用者,並更新您所屬的訂用帳戶清單。

    # sign in as a different user
    az login --user <myAlias@myCompany.com> --password <myPassword>
    
    # sign in with a different tenant
    az login --tenant <myTenantID>
    

    如果您的組織需要多重要素驗證,當您使用 az login --user時可能會收到此錯誤:

    Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access...
    

    使用替代 az login --tenant 命令會提示您開啟 HTTPS 頁面,並輸入所提供的程式代碼。 然後,您可以使用多重要素驗證並成功登入。 若要深入瞭解使用 Azure CLI 登入選項,請參閱 使用 Azure CLI 登入。

取得訂用帳戶資訊

大部分的 Azure CLI 命令都會在訂用帳戶內執行。 您可以使用 命令中的 參數,指定要在其中運作的 --subscription 訂用帳戶。 如果您未指定訂用帳戶,命令會使用您目前的作用中訂用帳戶。

若要查看您目前使用的訂用帳戶,或若要取得可用訂用帳戶的清單,請執行 az account showaz account list 命令。 移至瞭解如何 搭配 Azure CLI 使用 Bash,以查看更多使用這些命令的範例。

以下是示範如何取得訂用帳戶資訊的範例:

# get the current default subscription using show
az account show --output table

# get the current default subscription using list
az account list --query "[?isDefault]"

# get a subscription that contains search words or phrases
az account list --query "[?contains(name,'search phrase')].{SubscriptionName:name, SubscriptionID:id, TenantID:tenantId}" --output table

您也可以將訂用帳戶資訊儲存在變數中,以供腳本使用。

# store the default subscription in a variable
subscriptionId="$(az account list --query "[?isDefault].id" --output tsv)"
echo $subscriptionId

# store a subscription of certain name in a variable
subscriptionId="$(az account list --query "[?name=='my case sensitive subscription full name'].id" --output tsv)"
echo $subscriptionId

提示

參數 --output 是全局參數,適用於所有命令。 數據表值會以易記的格式呈現輸出。 如需詳細資訊,請參閱 Azure CLI 命令的輸出格式。

變更使用中的訂用帳戶

Azure 訂用帳戶同時具有名稱和標識碼。 您可以使用 az account set 來切換至不同的訂用帳戶,以指定所需的訂用帳戶標識碼或名稱。

# change the active subscription using the subscription name
az account set --subscription "My Demos"

# change the active subscription using the subscription ID
az account set --subscription "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

您也可以使用變數來變更訂用帳戶。 以下是範例:

# change the active subscription using a variable
subscriptionId="$(az account list --query "[?name=='my case sensitive subscription full name'].id" --output tsv)"
az account set --subscription $subscriptionId

如果您變更為位於不同租用戶的訂用帳戶,您也會變更作用中的租使用者。 若要瞭解如何將新的訂用帳戶新增至 Microsoft Entra 租使用者,請參閱 將 Azure 訂用帳戶關聯或新增至您的 Microsoft Entra 租使用者

如果您收到 「... 的訂用帳戶...不存在...錯誤,請參閱 可能解決方案的疑難解答

建立 Azure 管理群組

Azure 管理群組包含訂用帳戶。 管理群組提供管理這些訂用帳戶的存取、原則和合規性的方式。 如需詳細資訊,請參閱 什麼是 Azure 管理群組

使用 az account management-group 命令來建立和管理 Azure 管理群組。

您可以使用 az account management-group create 命令,為數個訂用帳戶建立管理群組:

az account management-group create --name Contoso01

若要查看您的所有管理群組,請使用 az account management-group list 命令:

az account management-group list

使用 az account management-group subscription add 命令,將訂用帳戶新增至新群組:

az account management-group subscription add --name Contoso01 --subscription "My Demos"
az account management-group subscription add --name Contoso01 --subscription "My Second Demos"

若要移除訂用帳戶,請使用 az account management-group subscription remove 命令:

az account management-group subscription remove --name Contoso01 --subscription "My Demos"

若要移除管理群組,請執行 az account management-group delete 命令:

az account management-group delete --name Contoso01

拿掉訂用帳戶或刪除管理群組並不會刪除或停用訂用帳戶。

設定 Azure 訂用帳戶鎖定

身為系統管理員,您可能需要鎖定訂用帳戶,以防止使用者刪除或修改訂閱。 如需詳細資訊,請參閱鎖定資源以防止非預期的變更

在 Azure CLI 中,使用 az account lock 命令。 例如, az account lock create 命令可以防止使用者刪除訂用帳戶:

az account lock create --name "Cannot delete subscription" --lock-type CanNotDelete

注意

您必須擁有 contributor 訂用帳戶的許可權,才能建立或變更鎖定。

若要查看您訂用帳戶上的目前鎖定,請使用 az account lock list 命令:

az account lock list --output table

如果您將帳戶設為唯讀,結果會類似於將讀取者角色的許可權指派給所有使用者。 若要瞭解如何設定個別使用者和角色的許可權,請參閱 使用 Azure CLI 新增或移除 Azure 角色指派。

若要查看鎖定的詳細數據,請使用 az account lock show 命令:

az account lock show --name "Cannot delete subscription"

您可以使用 az account lock delete 命令來移除鎖定

az account lock delete --name "Cannot delete subscription"

疑難排解

訂用帳戶不存在

除了印刷錯誤之外,您也可以在發生許可權計時問題時收到此錯誤。 例如,如果您在目前的終端機窗口開啟時已獲授與新訂用帳戶的許可權,就可能發生此錯誤。 解決方案是關閉並重新開啟終端機視窗,或使用 az logout 然後 az login 重新整理可用的訂用帳戶清單。

以下是可協助您尋找和變更訂用帳戶的腳本。

# See what subscription you are currently using.
az account show

# Get a list of available subscriptions.
az account list --output table

# If the subscription you are seeking is not in the list
#   close and reopen your terminal window,
#   or logout and then sign in again.
az logout
az login

# Did your available subscription list change?
az account list --output table

# If the subscription you are seeking is still not in the list,
#    contact your system administrator. You cannot change your
#    subscription to an ID that is not in the list.

# If the subscription you are seeking is now in the list,
#   change your subscription.
az account set --subscription 00000000-0000-0000-0000-00000000000

另請參閱