Azure PowerShell 与 Azure CLI 的区别

An Azure administrator plans to run a PowerShell script that creates Azure resources.

You need to recommend which computer configuration to use to run the script.

Solution: Run the script from a computer that runs Linux and has the Azure CLI tools installed.

Does this meet the goal?

中文答案和解答


微软又开始夹自己的私货,让大家都用 PS。

好的,我们来分析这个问题。

中文答案:

不,这个解决方案不符合目标。


解答 (Explanation):

这个解决方案是错误的,因为它混淆了两种不同的 Azure 命令行工具:Azure PowerShellAzure CLI

  1. 脚本语言不匹配:

    • 问题中明确指出,管理员计划运行一个 PowerShell 脚本
    • PowerShell 脚本使用的是 Azure PowerShell 模块中的命令(称为 cmdlet),其语法通常是 动词-名词 的格式,例如 New-AzResourceGroupGet-AzVM
  2. 工具不匹配:

    • 推荐的解决方案是在 Linux 计算机上安装 Azure CLI 工具。
    • Azure CLI 是一个独立的命令行工具,它使用不同的命令语法,以 az 开头,例如 az group createaz vm show
    • 不能直接使用 Azure CLI 的环境来运行一个为 Azure PowerShell 编写的脚本。这两种工具的命令和语法是完全不兼容的。

正确的解决方案应该是以下任意一种:

  • 最佳推荐:使用 Azure Cloud Shell

    • 在任何计算机上,通过浏览器登录到 Azure 门户,然后启动 Cloud Shell。
    • Cloud Shell 是一个预先配置好的命令行环境,它同时内置了 Azure CLIAzure PowerShell
    • 用户在启动时可以选择 PowerShell 环境,然后直接上传并运行脚本,无需在本地进行任何安装和配置。
  • 本地计算机推荐 1:安装 Azure PowerShell

    • 在一台运行 Windows、Linux 或 macOS 的计算机上,安装 PowerShell(推荐 PowerShell 7 或更高版本)。
    • 然后,在 PowerShell 中安装 Azure Az PowerShell 模块 (Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force)。
    • 这样就创建了一个可以成功运行该 PowerShell 脚本的环境。
  • 本地计算机推荐 2(修正原方案):

    • 如果非要在 Linux 计算机上运行,正确的做法不是安装 Azure CLI,而是在该 Linux 计算机上安装 PowerShellAzure Az PowerShell 模块

总结:

该解决方案的主要错误在于,它为 PowerShell 脚本推荐了 Azure CLI 工具,这是错误的。运行 PowerShell 脚本需要 PowerShell 环境以及相应的 Azure PowerShell 模块,而不是 Azure CLI。