Share via


CLI 範例:在使用者訂用帳戶模式中建立 Batch 帳戶

此指令碼會在使用者訂用帳戶模式中建立 Azure Batch 帳戶。 將計算節點配置到您的訂用帳戶的帳戶必須透過 Microsoft Entra 權杖驗證。 配置的計算節點會計入您訂用帳戶的 vCPU (核心) 配額。

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

必要條件

範例指令碼

啟動 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'

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

執行指令碼

# Create a Batch account in user subscription mode

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
[[ "$RESOURCE_GROUP" == '' ]] && resourceGroup="msdocs-batch-rg-$randomIdentifier" || resourceGroup="${RESOURCE_GROUP}"
tag="create-account-user-subscription"
keyVault="msdocskeyvault$randomIdentifier"
batchAccount="msdocsbatch$randomIdentifier"

# Allow Azure Batch to access the subscription (one-time operation).
az role assignment create --assignee ddbf3205-c6bd-46ae-8127-60eb93363864 --role contributor

# Create a resource group
echo "Creating $resourceGroup in "$location"..."
az group create --name $resourceGroup --location "$location" --tag $tag

# Create an Azure Key Vault. A Batch account that allocates pools in the user's subscription 
# must be configured with a Key Vault located in the same region.
echo "Creating $keyVault" 
az keyvault create --resource-group $resourceGroup --name $keyVault --location "$location" --enabled-for-deployment true --enabled-for-disk-encryption true --enabled-for-template-deployment true

# Add an access policy to the Key Vault to allow access by the Batch Service.
az keyvault set-policy --resource-group $resourceGroup --name $keyVault --spn ddbf3205-c6bd-46ae-8127-60eb93363864 --key-permissions all --secret-permissions all

# Create the Batch account, referencing the Key Vault either by name (if they
# exist in the same resource group) or by its full resource ID.
echo "Creating $batchAccount"
az batch account create --resource-group $resourceGroup --name $batchAccount --location "$location" --keyvault $keyVault 

# Authenticate directly against the account for further CLI interaction.
# Batch accounts that allocate pools in the user's subscription must be
# authenticated via an Azure Active Directory token.
az batch account login -g $resourceGroup -n $batchAccount

清除資源

您可以使用下列命令來移除資源群組及所有與其相關聯的資源,除非您仍持續需要這些資源,否則請使用 az group delete 命令。 某些資源可能需要一些時間才能建立或刪除。

az group delete --name $resourceGroup

範例參考

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

Command 注意
az role assignment create 為使用者、群組或服務主體建立新的角色指派。
az group create 建立用來存放所有資源的資源群組。
az keyvault create 建立金鑰保存庫。
az keyvault set-policy 更新指定的 Key Vault 的安全性原則。
az batch account create 建立 Batch 帳戶。
az batch account login 對指定的 Batch 帳戶驗證以進行進一步的 CLI 互動。
az group delete 刪除資源群組,包括所有的巢狀資源。

下一步

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