開始使用 Azure CLIGet started with Azure CLI

歡迎使用 Azure CLI!Welcome to the Azure CLI! CLI 工具旨在讓您快速且有效率地使用 Azure 服務,尤其著重在自動化的部分。The CLI is a tool designed to get you working quickly and efficiently with Azure services, with an emphasis on automation. 本文會介紹 CLI 的功能,以及可協助您提升工作成效的外部資源連結。This article introduces features of the CLI and links out to resources that help you be productive.

在 Azure Cloud Shell 中安裝或執行Install or run in Azure Cloud Shell

要開始使用 Azure CLI,最簡單的方式是在 Azure Cloud Shell 環境中透過瀏覽器加以執行。The easiest way to get started with the Azure CLI is by running it in an Azure Cloud Shell environment through your browser. 若要了解 Cloud Shell,請參閱 Azure Cloud Shell 中 Bash 的快速入門To learn about Cloud Shell, see Quickstart for Bash in Azure Cloud Shell.

當您準備好要安裝 CLI 時,請參閱安裝指示When you're ready to install the CLI, see the installation instructions.

第一次安裝 CLI 後,請執行 az --version 來確認已安裝完成,而且版本正確。After installing the CLI for the first time, check that it's installed and you've got the correct version by running az --version.

登入Sign in

在透過本機安裝使用任何 CLI 命令之前,您必須使用 az login 登入。Before using any CLI commands with a local install, you need to sign in with az login.

  1. 執行 login 命令。Run the login command.

    az login
    

    如果 CLI 可以開啟預設瀏覽器,它會執行這項操作,並載入登入頁面。If the CLI can open your default browser, it will do so and load a sign-in page.

    否則,您需要開啟瀏覽器頁面,並遵循命令列中的指示,在瀏覽器中瀏覽至 https://aka.ms/devicelogin 之後,輸入授權碼。Otherwise, you need to open a browser page and follow the instructions on the command line to enter an authorization code after navigating to https://aka.ms/devicelogin in your browser.

  2. 請在瀏覽器中使用您的帳戶認證登入。Sign in with your account credentials in the browser.

登入之後,您會看到一份與 Azure 帳戶相關聯的訂用帳戶清單。After logging in, you see a list of subscriptions associated with your Azure account. 包含 isDefault: true 的訂用帳戶資訊是目前在登入之後會啟動的訂用帳戶。The subscription information with isDefault: true is the currently activated subscription after logging in. 若要選取另一個訂用帳戶,請使用 az account set 命令搭配訂用帳戶識別碼以進行切換。To select another subscription, use the az account set command with the subscription ID to switch to. 如需訂用帳戶選項的詳細資訊,請參閱使用多個 Azure 訂用帳戶For more information about subscription selection, see Use multiple Azure subscriptions.

非互動式的登入方法有很多種,透過 Azure CLI 登入中有詳細說明。There are ways to sign in non-interactively, which are covered in detail in Sign in with Azure CLI.

常見命令Common commands

下表列出 CLI 中使用的一些常用命令,以及其參考文件的連結。This table lists some common commands used in the CLI and links to their reference documentation.

資源類型Resource type Azure CLI 命令群組Azure CLI command group
資源群組Resource group az groupaz group
虛擬機器Virtual machines az vmaz vm
儲存體帳戶Storage accounts az storage accountaz storage account
金鑰保存庫Key Vault az keyvaultaz keyvault
Web 應用程式Web applications az webappaz webapp
SQL 資料庫SQL databases az sql serveraz sql server
CosmosDBCosmosDB az cosmosdbaz cosmosdb

尋找命令Finding commands

CLI 中的命令會以「群組」的「命令」形式組織。Commands in the CLI are organized as commands of groups. 每個群組都代表一項 Azure 服務,以及該服務上運作的命令。Each group represents an Azure service, and commands operate on that service.

若要搜尋命令,請使用 az findTo search for commands, use az find. 例如,若要搜尋包含 secret 的命令名稱,請使用下列命令:For example, to search for command names containing secret, use the following command:

az find secret

使用 --help 引數取得命令的完整清單和群組的子群組。Use the --help argument to get a complete list of commands and subgroups of a group. 例如,若要尋找使用網路安全性群組 (NSG) 的 CLI 命令:For example, to find the CLI commands for working with Network Security Groups (NSGs):

az network nsg --help

針對 bash 殼層下的命令,CLI 有完整的 tab 鍵自動完成。The CLI has full tab completion for commands under the bash shell.

全域可用的引數Globally available arguments

有一些引數可供每個命令使用。There are some arguments that are available for every command.

  • --help 會輸出有關命令的 CLI 參考資訊命令和其引數,並列出可用的子群組和命令。--help prints CLI reference information about commands and their arguments and lists available subgroups and commands.
  • --output 會變更輸出格式。--output changes the output format. 可用的輸出格式為 jsonjsonc (以色彩標示的 JSON)、tsv (定位字元分隔值)、table (人類看得懂的 ASCII 資料表) 和 yamlThe available output formats are json, jsonc (colorized JSON), tsv (Tab-Separated Values), table (human-readable ASCII tables), and yaml. 根據預設,CLI 會輸出 jsonBy default the CLI outputs json. 若要深入了解可用的輸出格式,請參閱 Azure CLI 的輸出格式To learn more about the available output formats, see Output formats for Azure CLI.
  • --query 會使用 JMESPath 查詢語言來篩選從 Azure 服務傳回的輸出。--query uses the JMESPath query language to filter the output returned from Azure services. 若要了解有關查詢的詳細資訊,請參閱使用 Azure CLI 查詢命令結果JMESPath 教學課程To learn more about queries, see Query command results with Azure CLI and the JMESPath tutorial.
  • --verbose 會輸出作業執行時,在 Azure 中建立的資源相關資訊,以及其他有用的資訊。--verbose prints information about resources created in Azure during an operation, and other useful information.
  • --debug 會輸出更多 CLI 作業的詳細資訊,以用於偵錯。--debug prints even more information about CLI operations, used for debugging purposes. 如果您發現錯誤,可在提交錯誤報告時提供使用 --debug 旗標產生的輸出。If you find a bug, provide output generated with the --debug flag on when submitting a bug report.

互動模式Interactive mode

CLI 提供互動模式,會自動顯示說明資訊,並可讓您更輕鬆地選取子命令。The CLI offers an interactive mode that automatically displays help information and makes it easier to select subcommands. 使用 az interactive 命令進入互動模式。You enter interactive mode with the az interactive command.

az interactive

如需互動模式的詳細資訊,請參閱 Azure CLI 互動模式For more information on interactive mode, see Azure CLI Interactive Mode.

另外還有 Visual Studio Code 外掛程式,提供互動式體驗,包括自動完成和滑鼠移過即顯示內容的文件。There's also a Visual Studio Code plugin that offers an interactive experience, including autocomplete and mouse-over documentation.

透過快速入門和教學課程了解 CLI 基本概念Learn CLI basics with quickstarts and tutorials

若要開始使用 Azure CLI,請嘗試透過深入的教學課程來設定虛擬機器,以及使用 CLI 的強大功能查詢 Azure 資源。To get you started with the Azure CLI, try an in-depth tutorial for setting up virtual machines and using the power of the CLI to query Azure resources.

另外還有其他熱門服務的快速入門。There are also quickstarts for other popular services.

提供意見反應Give feedback

歡迎您提供有關 CLI 的意見反應,以協助我們進行改善及解決錯誤。We welcome your feedback for the CLI to help us make improvements and resolve bugs. 您可以在 GitHub 上提交問題或使用 CLI 的內建功能,以 az feedback 命令留下一般意見反應。You can file an issue on GitHub or use the built-in features of the CLI to leave general feedback with the az feedback command.

az feedback