備妥環境以使用 Azure CLI

在本教學課程步驟中,您會執行您的第一個 Azure CLI 命令! 這包括尋找並設定作用中的訂用帳戶,以及設定預設值。 您也會瞭解如何建立包含隨機標識符的資源群組,以一律保證唯一的資源名稱。

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

若要完成本教學課程步驟,您需要 contributor 或更高的訂用帳戶許可權。

安裝 Azure CLI

先選取您的環境,即可開始使用。

  • 選取每個 Azure CLI 程式代碼區塊右上角的 [開啟 Cloud Shell] 按鈕,即可使用 Azure Cloud Shell 中的 Bash 環境。

  • 如果您想要在本機執行 Azure CLI 參考命令,請 安裝 Azure CLI。

Azure CLI 的目前版本是 2.59.0。 如需最新版本的相關信息,請參閱 版本資訊。 若要尋找已安裝的版本,並查看是否需要更新,請執行 az version

使用 Azure CLI 登入 Azure

使用 Azure CLI 時,有數 個驗證選項 。 Azure CLI 登入的預設驗證方法會使用網頁瀏覽器和存取令牌來登入。

  1. 執行 az login 命令。

    az login
    

    如果 Azure CLI 可以開啟您的預設瀏覽器,它會起始 授權碼流程 ,並開啟預設瀏覽器以載入 Azure 登入頁面。

    否則,它會起始 裝置程式代碼流程 ,並指示您在 開啟瀏覽器頁面 https://aka.ms/devicelogin。 然後,輸入終端機中顯示的程序代碼。

    如果沒有可用的網頁瀏覽器,或網頁瀏覽器無法開啟,您可以使用 az login --use-device-code 強制裝置程式代碼流程

  2. 請在瀏覽器中使用您的帳戶認證登入。

尋找並變更作用中的訂用帳戶

登入 Azure CLI 之後,請一律檢查您目前的訂用帳戶。 如果您未在偏好的訂用帳戶下運作,請使用 az account set 變更您的訂用帳戶。 以下是要使用的程式代碼範例:

# see your current/default subscription
az account show

# find the list of subscriptions available to you
az account list --output table

# change your current/default subscription
az account set --subscription <mySubscriptionName>

# you can also set your subscription using a subscription ID
az account set --subscription <00000000-0000-0000-0000-000000000000>

命令的 az account show 主控台輸出:

{
  "environmentName": "AzureCloud",
  "homeTenantId": "00000000-0000-0000-0000-000000000000",
  "id": "00000000-0000-0000-0000-000000000000",
  "isDefault": true,
  "managedByTenants": [],
  "name": "Your storage account name",
  "state": "Enabled",
  "tenantId": "00000000-0000-0000-0000-000000000000",
  "user": {
    "name": "yourName@contoso.com",
    "type": "user"
  }
}

了解資源群組

資源群組是 Azure 資源的容器。 若要建立資源群組,您需要 contributor 訂用帳戶中的許可權或更高許可權。

建立資源群組

  1. 在訂用帳戶內,資源組名必須是唯一的。 請先檢查您想要的名稱是否可使用 az group exists 命令。 的 false 輸出值表示名稱可供使用。

    az group exists --name <myUniqueRGname>
    
  2. 使用 az account list-locations 命令擷取訂用帳戶支持的區域清單。 如需可用的 Azure 位置清單,請參閱 為您選擇正確的 Azure 區域。

    az account list-locations --query "[].{Region:name}" --output table
    
  3. 是時候建立您的資源群組了! 使用 az group create 命令。

    az group create --location <myLocation> --name <myUniqueRGname>
    

建立包含隨機標識碼的資源群組

測試時,最好建立資源群組,其中包含名稱中的隨機標識碼。 將隨機標識元新增至資源組名可讓您重新測試程序代碼,而不需要等候先前同名的資源群組從 Azure 中移除。

Bash 和 PowerShell 變數語法不同。 為您的環境複製正確的腳本。

let "randomIdentifier=$RANDOM*$RANDOM"
location="eastus"
resourceGroup="msdocs-tutorial-rg-$randomIdentifier"
az group create --name $resourceGroup --location $location --output json

Bash 和 PowerShell 控制台輸出:

{
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/msdocs-tutorial-rg-000000000",
  "location": "eastus",
  "managedBy": null,
  "name": "msdocs-tutorial-rg-000000000",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}

設定環境變數

Azure CLI 提供數個選項,可讓您重複使用一般參數值。 這些預設值會儲存在環境變數中,而且每個登入都是唯一的。

  1. 設定您的預設資源群組。

    az config set defaults.group=<msdocs-tutorial-rg-0000000>
    
  2. 一次設定多個環境變數。

    許多 Azure CLI 參數會採用以空格分隔的多個值。 組態值是一個這類實例。 下一個範例會設定.location每個 Azure CLI 命令的 --location 和 參數所使用的 和 --resource-group.group 預設值。

    az config set defaults.location=westus2 defaults.group=<msdocs-tutorial-rg-0000000>
    
  3. 設定您的預設輸出。

    當您選擇在 Azure Cloud Shell 中工作,或在本機安裝 Azure CLI 時,預設輸出會自動設定為 json。 不過,這是要了解和設定的最重要預設值之一。 輸出會決定控制台上出現的內容,以及寫入記錄檔的內容。 當您建立傳回金鑰、密碼和秘密的資源時,請一律使用的輸出 none

    az config set core.output=none
    

    在本教學課程中,我們不會使用秘密。 將預設值設定回 , json 讓您可以在本教學課程中看到每個參考命令的傳回輸出。

    az config set core.output=json
    
  4. 瞭解如何使用 az init

    Azure CLI 有參考命令,會逐步引導您設定環境。 輸入您的 az init 主控台,然後按 Enter。 遵循提供的提示。

    az init
    

    az init 的第一件好事是它會為您提供您所有目前的設定! 以下是範例輸出:

    Your current config settings:
    
      Output format: JSON 
      [core.output = json]
    
      Standard error stream (stderr): All events 
      [core.only_show_errors = false]
    
      Error output: Show recommendations 
      [core.error_recommendation = on]
    
      Syntax highlighting: On 
      [core.no_color = false]
    
      Progress Bar: On 
      [core.disable_progress_bar = false]
    
    
    Select an option by typing it's number
    
      [1] Optimize for interaction
          These settings improve the output legibility and optimize for human interaction
    
      [2] Optimize for automation
          These settings optimize for machine efficiency
    
      [3] Customize settings
          A walk through to customize common configurations
    
      [4] Exit (default)
          Return to the command prompt
    
    ? Your selection: 
    
  5. 尋找並讀取您的組態檔。

    如果您以「信任但驗證」的心態工作,您會想要知道您的組態檔儲存位置及其包含的內容。 組態檔本身位於 $AZURE_CONFIG_DIR/config。 的預設值 AZURE_CONFIG_DIR 位於 $HOME/.azure Linux和macOS上,以及 %USERPROFILE%\.azure Windows 上。 立即尋找您的組態檔,並查看其包含的內容。

取得更多詳細數據

您要針對本教學課程步驟中涵蓋的其中一個主題提供更多詳細數據嗎? 使用此表格中的連結來深入瞭解。

主體 深入了解
環境 選擇正確的 Azure 命令列工具
登入選項 使用 Azure CLI 登入
詞彙 Azure CLI 術語和支持層級
訂用帳戶 使用 Azure CLI 管理訂用帳戶
資源群組 使用 Azure CLI 管理資源群組
設定 設定 Azure CLI
Azure 角色 Azure 角色、Microsoft Entra 角色和傳統訂用帳戶管理員角色

後續步驟

既然您已瞭解如何設定環境,請繼續進行下一個步驟,以瞭解Bash、PowerShell和 Cmd 之間的腳本差異。