Azure CLI 简介

已完成

Azure CLI 是一个跨平台的命令行工具,用于连接到 Azure 并对 Azure 资源运行管理命令。 通过使用交互式命令行提示或脚本,可使用它通过终端运行命令。

若要进行交互式使用,请先打开一个 shell,例如:

  • Windows、Linux 或 macOS 上的 PowerShell。
  • Windows 上的 Cmd.exe
  • 在 Linux 或 macOS 上运行 Bash。

然后在 shell 提示符下发出命令。 若要自动执行重复性任务,请使用所选 shell 的语法将 Azure CLI 命令汇编到 shell 脚本中,然后运行该脚本。

可以在 Windows、Linux、macOS 上本地安装 Azure CLI。 还可以通过 Azure Cloud Shell 在浏览器中使用它,或者从 Docker 容器内部运行它。

Microsoft 文档对 Bash shell 的 Azure CLI 脚本进行了标准化,我们会在此处执行相同的操作。 请记住,如果选择使用 PowerShell 或 cmd.exe shell,则在复制 Bash 脚本以在其他 shell 中使用时,存在轻微的脚本差异(例如换行符或引号)。

先决条件

由于你的公司已使用 Azure,因此你有一个有效的 Azure 订阅。 你在 Azure Cloud Shell 中使用 Bash。

创建资源组

创建存储帐户前,需要创建资源组或使用现有资源组。 资源组是在其中以组的形式部署和管理 Azure 资源的逻辑容器。

使用 storageaccountexamplerg 命令在 eastus 区域中创建名为 az group create 的 Azure 资源组:

az group create -name storageaccountexamplerg -___location eastus

创建存储帐户

存储帐户是一种 Azure 资源,包含在资源组中。 存储帐户名称的长度必须为 3 到 24 个字符,并且只能包含数字和小写字母。 存储帐户名称在 Azure 中必须是唯一的。 没有两个存储帐户可以有相同的名称。

要在 Azure 中创建存储帐户,需要知道要创建的存储的位置、SKU 和类型。 下面是用于创建存储帐户的简单脚本:

# check if the storage account name is available
az storage account check-name --name <storage-account-name>

# create the storage account
az storage account create \
  --name <storage-account-name> \
  --resource-group storageaccountexamplerg \
  --___location eastus \
  --sku Standard_RAGRS \
  --kind StorageV2

验证存储帐户

对于许多 Azure 资源,Azure CLI 提供了 list 子命令来查看资源详细信息。 使用 Azure CLI az storage account list 命令返回有关在上一步中创建的存储帐户的信息:

# Get a list of all storage accounts in the active subscription
az storage account list

# Get a list of all storage accounts for a resource group
az storage account list --resource-group storageaccountexamplerg

清理资源

使用 az group delete 命令删除资源组。 唯一的强制参数是 name。 删除资源组时会一并删除该资源组及其包含的所有资源。 如果 storageaccountexamplerg 资源组中存在本单元中创建的存储帐户范围之外的资源,这些资源也会被删除。

az group delete --name storageaccountexamplerg

在与其他团队成员共享的资源组中工作时,请使用 az storage account delete 命令删除测试存储帐户:

az storage account delete --name <storage-account-name>