使用最新的 API 以程式設計方式建立 Azure Enterprise 合約訂用帳戶

本文可協助您使用最新的 API 版本,以程式設計方式建立 EA 計費帳戶的 Azure Enterprise 合約 (EA) 訂用帳戶。 如果您仍在使用較舊的預覽版本,請參閱 以程序設計方式建立 Azure 訂用帳戶舊版 API

在本文中,可了解如何使用 Azure Resource Manager 以程式設計方式建立訂用帳戶。

當您以程式設計方式建立 Azure 訂用帳戶時,它屬於您從 Microsoft 或經認證的賣方收到 Azure 服務的合約條款。 如需詳細資訊,請參閱 Microsoft Azure 法律資訊

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

您無法以程式設計方式建立支援計劃。 您可以在 Azure 入口網站中購買新的支援計劃或升級計劃。 瀏覽至 [說明 + 支援],然後在頁面頂端選取 [選擇正確的支援計劃]

必要條件

使用者必須具有註冊帳戶的企業系統管理員角色或擁有者角色,才能建立訂用帳戶。 有兩種方式可取得註冊帳戶的擁有者角色:

若要使用服務主體來建立 EA 訂用帳戶,註冊帳戶的擁有者必須授與該服務主體建立訂用帳戶的能力

使用服務主體建立訂用帳戶時,請使用 Microsoft Entra Enterprise 應用程式的 ObjectId 作為使用 Microsoft Graph PowerShellAzure CLI 的服務主體識別碼。 您也可以使用尋找服務主體和租用戶識別碼中的步驟,在現有服務主體的 Azure 入口網站中尋找物件識別碼。

如需 EA 角色指派 API 要求的詳細資訊,請參閱將角色指派給 Azure Enterprise 合約服務主體名稱。 本文包含可指派給服務主體的角色 (和角色定義識別碼) 的清單。

注意

  • 請確定您使用正確的 API 版本來授與註冊帳戶擁有者權限。 針對本文及其中所述的 API,請使用 2019-10-01-preview API。
  • 若您正在移轉至更新的 API,您之前使用 2015-07-01 版本進行的設定不會自動轉換為使用較新的 API。
  • 僅有在使用者的角色為帳戶擁有者時,系統才會顯示註冊帳戶資訊。 若使用者有多個角色,API 會使用使用者最不嚴格的角色。

尋找您可以存取的帳戶

當您加入與「帳戶擁有者」相關聯的註冊帳戶之後,Azure 會使用帳戶與註冊的關聯性來判斷由誰支付訂用帳戶的費用。 所有在該帳戶下所建立訂用帳戶的費用,都會由該帳戶所在的 EA 註冊支付。 若要建立訂用帳戶,您必須傳入註冊帳戶與使用者主體的相關值,才能擁有訂用帳戶。

若要執行下列命令,您必須登入「帳戶擁有者」的主目錄 (建立訂用帳戶時預設的所在目錄)。

要求列出您有權存取的所有註冊帳戶:

GET https://management.azure.com/providers/Microsoft.Billing/billingaccounts/?api-version=2020-05-01

API 回應會列出您有權存取的所有註冊帳戶:

{
  "value": [
    {
      "id": "/providers/Microsoft.Billing/billingAccounts/1234567",
      "name": "1234567",
      "properties": {
        "accountStatus": "Unknown",
        "accountType": "Enterprise",
        "agreementType": "EnterpriseAgreement",
        "soldTo": {
          "companyName": "Contoso",
          "country": "US "
        },
        "billingProfiles": {
          "hasMoreResults": false
        },
        "displayName": "Contoso",
        "enrollmentAccounts": [
          {
            "id": "/providers/Microsoft.Billing/billingAccounts/1234567/enrollmentAccounts/7654321",
            "name": "7654321",
            "type": "Microsoft.Billing/enrollmentAccounts",
            "properties": {
              "accountName": "Contoso",
              "accountOwnerEmail": "kenny@contoso.onmicrosoft.com",
              "costCenter": "Test",
              "isDevTest": false
            }
          }
        ],
        "hasReadAccess": false
      },
      "type": "Microsoft.Billing/billingAccounts"
    }
  ]
}

計費範圍和 id 的值相同。 註冊帳戶的 id 是起始訂用帳戶要求的計費範圍。 請務必記下識別碼,因為這是您稍後在本文中建立訂用帳戶時會用到的必要參數。

在特定的註冊帳戶下建立訂用帳戶

下列範例會在上一個步驟中選取的註冊帳戶中,建立名為「開發小組訂用帳戶」的訂用帳戶。

使用下列其中一種方法,您可以建立訂用帳戶別名名稱。 建議您在建立別名名稱時:

  • 使用英數字元與連字號
  • 以英文字母開頭,並以英數字元結尾
  • 請勿使用英文句點

別名是用來簡單替代使用者定義的字串,而非替代訂用帳戶 GUID。 換句話說,您可以將其當作捷徑使用。 您可以在 [別名 - 建立]中深入了解別名。 在以下範例中會建立 sampleAlias,但您可以使用任何您想要的字串。

若您除了帳戶擁有者角色外,還有多個使用者角色,則必須在 Azure 入口網站擷取帳戶識別碼。 然後,您可以使用識別碼以程式設計方式建立訂用帳戶。

呼叫 PUT API 以建立訂用帳戶建立要求/別名。

PUT  https://management.azure.com/providers/Microsoft.Subscription/aliases/sampleAlias?api-version=2021-10-01

在要求本文中,提供您其中一個 enrollmentAccountsid 作為 billingScope

{
  "properties": {
        "billingScope": "/providers/Microsoft.Billing/BillingAccounts/1234567/enrollmentAccounts/7654321",
        "DisplayName": "Dev Team Subscription", //Subscription Display Name
        "Workload": "Production"
  }
}

Workload 的允許值為 ProductionDevTest

回應

{
  "id": "/providers/Microsoft.Subscription/aliases/sampleAlias",
  "name": "sampleAlias",
  "type": "Microsoft.Subscription/aliases",
  "properties": {
    "subscriptionId": "b5bab918-e8a9-4c34-a2e2-ebc1b75b9d74",
    "provisioningState": "Accepted"
  }
}

您可以在相同的 URL 上執行 GET,以取得要求的狀態。

要求

GET https://management.azure.com/providers/Microsoft.Subscription/aliases/sampleAlias?api-version=2021-10-01

回應

{
  "id": "/providers/Microsoft.Subscription/aliases/sampleAlias",
  "name": "sampleAlias",
  "type": "Microsoft.Subscription/aliases",
  "properties": {
    "subscriptionId": "b5bab918-e8a9-4c34-a2e2-ebc1b75b9d74",
    "provisioningState": "Succeeded"
  }
}

此時會傳回進行中狀態,在 provisioningState 下顯示為 Accepted 狀態。

建立訂用帳戶並讓subscriptionOwnerId成為擁有者

當服務主體使用訂用帳戶別名 API 來建立新的訂用帳戶,且不包含 additionalProperties 在要求中時,服務主體會自動成為新訂用帳戶的擁有者。 如果您不想讓服務主體成為擁有者,您可以在 中additionalProperties指定 subscriptionTenantIdsubscriptionOwnerId 。 此程式會讓指定的 subscriptionOwnerId 新訂用帳戶擁有者,而不是服務主體。

範例要求本文:


{
    "properties": {
        "billingScope": "/providers/Microsoft.Billing/billingAccounts/{EABillingAccountId}/enrollmentAccounts/{EnrollmentAccountId}",
        "displayName": "{SubscriptionName}",
        "workLoad": "Production",
        "resellerId": null,
        "additionalProperties": {
            "managementGroupId": "",
            "subscriptionTenantId": "{SubscriptionTenantId}", // Here you input the tenant GUID where the subscription resides after creation
            "subscriptionOwnerId": "{ObjectId that becomes the owner of the subscription}", // Here you input the objectId which is set as the subscription owner when it gets created.
            "tags": {}
        }
    }
}

在不同的租使用者中建立訂用帳戶

使用訂 用帳戶別名 REST API,您可以使用要求本文中的 參數,在不同的租使用者中建立訂用 subscriptionTenantId 帳戶。 您的 Azure 服務主體 (SPN) 必須從其主租使用者取得令牌,才能建立訂用帳戶。 建立訂用帳戶之後,您必須從目標租使用者取得令牌,才能使用 接受擁有權 API 接受轉移。

如需關於在另一個租用戶中建立 EA 訂用帳戶的詳細資訊,請參閱在其他租用戶中建立訂用帳戶並檢視移轉要求

使用 ARM 範本或 Bicep

上一節示範了如何使用 PowerShell、CLI 或 REST API 建立訂閱。 如果您需要將訂閱建立過程自動化,請考慮使用 Azure Resource Manager 範本 (ARM 範本) 或 Bicep 檔案

下列 ARM 範本會建立訂閱。 若為 billingScope,請提供註冊帳戶識別碼。 訂閱會建立在根管理群組中。 建立訂閱之後,您可以將其移至另一個管理群組。

{
    "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "subscriptionAliasName": {
            "type": "string",
            "metadata": {
                "description": "Provide a name for the alias. This name will also be the display name of the subscription."
            }
        },
        "billingScope": {
            "type": "string",
            "metadata": {
                "description": "Provide the full resource ID of billing scope to use for subscription creation."
            }
        }
    },
    "resources": [
        {
            "scope": "/",
            "name": "[parameters('subscriptionAliasName')]",
            "type": "Microsoft.Subscription/aliases",
            "apiVersion": "2021-10-01",
            "properties": {
                "workLoad": "Production",
                "displayName": "[parameters('subscriptionAliasName')]",
                "billingScope": "[parameters('billingScope')]"
            }
        }
    ],
    "outputs": {}
}

或者,使用 Bicep 檔案來建立訂閱。

targetScope = 'managementGroup'

@description('Provide a name for the alias. This name will also be the display name of the subscription.')
param subscriptionAliasName string

@description('Provide the full resource ID of billing scope to use for subscription creation.')
param billingScope string

resource subscriptionAlias 'Microsoft.Subscription/aliases@2021-10-01' = {
  scope: tenant()
  name: subscriptionAliasName
  properties: {
    workload: 'Production'
    displayName: subscriptionAliasName
    billingScope: billingScope
  }
}

管理群組層級部署範本。 下列範例示範如何部署 JSON ARM 範本,但您可以改部署 Bicep 檔案。

PUT https://management.azure.com/providers/Microsoft.Management/managementGroups/mg1/providers/Microsoft.Resources/deployments/exampledeployment?api-version=2020-06-01

使用要求本文:

{
  "location": "eastus",
  "properties": {
    "templateLink": {
      "uri": "http://mystorageaccount.blob.core.windows.net/templates/template.json"
    },
    "parameters": {
      "subscriptionAliasName": {
        "value": "sampleAlias"
      },
      "billingScope": {
        "value": "/providers/Microsoft.Billing/BillingAccounts/1234567/enrollmentAccounts/7654321"
      }
    },
    "mode": "Incremental"
  }
}

若要將訂閱移至新的管理群組,請使用下列 ARM 範本。

{
    "$schema": "https://schema.management.azure.com/schemas/2019-08-01/managementGroupDeploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "targetMgId": {
            "type": "string",
            "metadata": {
                "description": "Provide the ID of the management group that you want to move the subscription to."
            }
        },
        "subscriptionId": {
            "type": "string",
            "metadata": {
                "description": "Provide the ID of the existing subscription to move."
            }
        }
    },
    "resources": [
        {
            "scope": "/",
            "type": "Microsoft.Management/managementGroups/subscriptions",
            "apiVersion": "2020-05-01",
            "name": "[concat(parameters('targetMgId'), '/', parameters('subscriptionId'))]",
            "properties": {
            }
        }
    ],
    "outputs": {}
}

或以下 Bicep 檔案。

targetScope = 'managementGroup'

@description('Provide the ID of the management group that you want to move the subscription to.')
param targetMgId string

@description('Provide the ID of the existing subscription to move.')
param subscriptionId string

resource subToMG 'Microsoft.Management/managementGroups/subscriptions@2020-05-01' = {
  scope: tenant()
  name: '${targetMgId}/${subscriptionId}'
}

Azure 企業版訂用帳戶建立 API 的限制

  • 使用此 API 只能建立「Azure 企業版」訂用帳戶。
  • 每個註冊帳戶的限制為5,000個。 之後,您只能在 Azure 入口網站中為帳戶建立更多訂用帳戶。 若要透過此 API 建立更多訂用帳戶,請建立另一個註冊帳戶。 已取消、已刪除和已轉移的訂用帳戶會計入 5000 筆的限制。
  • 不是帳戶擁有者,但已透過 Azure 角色型存取控制新增至註冊帳戶的使用者,無法在 Azure 入口網站 中建立訂用帳戶。

下一步