脚本编写环境

已完成

本单元介绍可用于管理 Azure 资源的命令行 shell。 我们会回顾一些可帮助你根据要求选择最佳命令行 shell 的概念。

什么是命令行 shell?

命令行 shell 是基于文本的程序,用于命令操作系统执行操作。 Bash、PowerShell、Windows PowerShell 和 cmd.exe 都是 shell 环境。 Shell 环境不仅决定你可以使用哪些工具,还会更改你的命令行体验。

Bash

Bash 是命令行解释器和 shell 脚本编写语言。 Bash 在 Linux 和 macOS 上运行。 Bash 也可通过适用于 Linux 的 Windows 子系统 (WSL) 在 Azure Cloud Shell 和 Windows 上使用。

PowerShell

PowerShell 是一种跨平台的任务自动化解决方案,由命令行 shell、脚本语言和配置管理框架组成。 PowerShell 在 Windows、Linux 和 macOS 上运行。 还可在 Azure Cloud Shell 中使用 PowerShell。

Azure Cloud Shell

Azure Cloud Shell 是在容器中运行 Linux 的托管 Shell 环境。 Cloud Shell 提供两种命令行 shell:

  • 预安装了 Azure CLI 的 Bash
  • 预安装了 Azure PowerShell 和 Azure CLI 的 PowerShell

Cloud Shell 可以在 Web 浏览器中访问,并集成了 Windows 终端和 Visual Studio Code (VS Code)。

支持的 shell 环境

下表确定了每个 Azure 命令行工具支持的 shell。 在表中:

  • 支持图标指示命令行工具在指定的 shell 中supported
  • 不支持图标指示命令行工具在指定的 shell 中not supported
Shell 环境 Azure CLI Azure PowerShell Bicep Terraform
Bash 支持 不支持 支持 支持
PowerShell 支持 受支持 受支持 支持
Windows PowerShell 支持 受支持 受支持 支持
Azure Cloud Shell 支持 受支持 受支持 受支持
cmd.exe 受支持 不支持 支持 支持

Azure CLI 具有在所有五个 shell 环境中使其命令可执行的安装程序。

Azure PowerShell 是打包成一个名为 Az 的 PowerShell 模块的 cmdlet 集。 它不是可执行文件。 必须使用 PowerShell 或 Windows PowerShell 在 Az PowerShell 模块中运行命令。

PowerShell 是使用 .NET Core 作为其运行时的独立安装,因此它可以安装在 Windows、Linux 和 macOS 上。 Windows PowerShell 是预安装在大多数 Windows 操作系统中的脚本编写环境 shell。

可以使用 PowerShell 或 Bash 来管理 Azure 资源。 最佳做法是针对特定 shell 编写脚本,因为 PowerShell 和 Bash 的语法元素不同。 例如,对于续行符,Bash 使用反斜杠 (\),而 PowerShell 使用反引号 (`)。 Shell 环境中的差异不会更改 Azure CLI 和 Azure PowerShell 的操作方式。 但它们确实会更改你的命令行体验。