Azure CLI 和 PowerShell 之间的区别

Difference between Azure CLI and PowerShell

我作为 Windows 用户问这个问题,但我请求您不要将答案限制为 windows。

我尝试在 Power-Shell 中执行 Azure CLI 命令,它们执行成功但反之则不然.在那种情况下,为什么我们有 2 个单独的命令集?为什么不只在 Power-Shell 上工作?我只尝试了一些基本命令,它们都可以正常工作,除了过滤命令 | find 仅适用于 CLI 和 | Select 仅适用于 Power-Shell.

我知道 Azure CLI 用于跨平台支持。但是 Windows 用户有什么区别吗? Power-Shell 上的 运行 CLI 命令是否有任何后果?

提前致谢。

这是 对两者的使用的意见。我绝不是说一个比另一个好。它们各有利弊。


Azure CLI是管理Azure资源的跨平台命令行工具,它可以运行在Windows、Mac和 Linux。这也意味着它可以 运行 on Windows PowerShell。它比 Azure PowerShell 更灵活,因为它是二进制文件,可以 运行 inside any OS default shell.

Are there any consequences to run CLI command on Power-Shell?

更新可能有点麻烦。如果您想在 Windows 上更新它,您必须按照 Install Azure CLI on Windows. Updating is easier on other platforms, and makes me only want to only use Azure PowerShell on Windows PowerShell. A work around is to use WSL on Windows, then you can run Azure CLI on Linux inside a Windows machine. You can install WSL at Windows Subsystem for Linux Installation Guide for Windows 10. I find updating the azure-cli package much easier on Linux using apt-get than the Windows equivalent. You can have a look at Install Azure CLI with apt 中关于如何在 Linux.

上安装 Azure CLI 包的说明重新安装 MSI

另一个区别是您必须使用 Azure CLI 链接多个命令,例如来自 Azure PowerShell 的 az group listGet-AzResourceGroup。您也不能像使用 Azure PowerShell 那样使用 Azure CLI 命令 运行 Get-Help,这对我来说是一个巨大的游戏规则改变者,因为我发现 PowerShell 帮助系统非常有助于深入显示有关 PowerShell Cmdlet 的信息。 Azure CLI 帮助信息可通过 az --help 找到,但不如 Get-Help.

全面

使用 Windows PowerShell 时,Tab 补全在 Azure CLI 中也不起作用。这也让打字变得有点痛苦,这是我在 Windows 中使用 WSL 的另一个原因。您可以在这个 Autocompletion support in Windows command line GitHub 问题上查看其他替代方案。


另一方面,

Azure PowerShell 是一组用于从命令行管理 Azure 资源的 PowerShell Cmdlet,并且 有效在 Windows PowerShell 和 PowerShell Core 中。这也意味着如果另一个 OS 例如 Mac 或 Linux 是 运行ning PowerShell Core,那么它也可以 运行 Azure PowerShell。

I have only tried some basic commands and they are all working except filter commands | find only works on CLI and | Select only works in Power-Shell.

Select-Object or the shorthand Select is a PowerShell Cmdlet, so it only works on PowerShell objects. find can search a string or text file. Furthermore, if you are searching for data from Azure CLI, you should use the --query parameter instead of find, since find is limited to searching strings inside text. Azure CLI uses the JMESPath 查询语言,用于在您收到的 JSON 输出中搜索数据。如果您熟悉这种查询语言,那么使用 Azure CLI 搜索数据应该不是什么大问题。此外,您还可以在 PowerShell 脚本中使用 Azure CLI 命令,但反之则不行。


总结

如果您处理多个平台或想与使用不同平台的其他人一起编写脚本,使用 Azure CLI 是一个不错的选择。但是,如果你主要处理 Windows 系统并与其他人一起工作,那么使用 Azure PowerShell 是个好主意。如果您像我一样必须使用不同的平台,那么安装两者是个好主意。如果您仍然只想在不同的 OS 平台上使用 Azure PowerShell,那么您需要 PowerShell Core。

对于简单的任务,例如在云中快速查找资源 shell 或编写快速脚本,Azure CLI 很好用,而且比 Azure PowerShell 更简洁。如果你已经经常使用 bash,这会感觉更自然,将 Azure CLI 命令添加到现有脚本将是一项简单的任务。正如其他人所说,没有什么能阻止您将 Azure CLI 命令添加到 powershell 脚本,这允许您使用 ConvertFrom-Json 将 JSON 输出反序列化为 PSCustomObject .

对于更复杂的任务,Azure PowerShell 更可取,因为使用 .NET objects/OOP 主体比解析 Azure CLI 给出的 JSON 文本要容易得多。这是我尽可能使用 Azure PowerShell 的原因之一。

Azure CLI 确实受益于幂等性,因此运行对资源使用相同的命令不需要像 Azure PowerShell 中那样进行任何空值检查。如果这成为一个问题,那么您可以 运行 Azure PowerShell 中的 ARM 模板,它们是幂等的。


更新

helpfully pointed out in the comments, PowerShell 7.0 has recently been released, replacing the name PowerShell Core or PowerShell 6.x. You can read more at Announcing PowerShell 7.0.

另一个想法

如果您想从 Azure PowerShell 维护 OOP 主体,但使用 Linux 系统管理员更容易使用的东西,那么您可以考虑使用 Azure SDK for Python。过去我想在 Linux 主机中使用 运行 脚本,但不想使用 Azure CLI 或安装 PowerShell。

现有答案是看待这个问题的愚蠢方式。和误导。

最大的区别是 azure cli 是 binary(可以 运行 在不同的平台上)而 Powershell 是 shell 可以跨平台工作。 Azure Powershell 是一堆 Powershell 模块,其他一切都源自于此。

find 无法在 cli 中工作,因为没有 in the cli,因为它不是 shell。 find 在 Windows 上的 Powershell 中工作得非常好,因为它是 Windows OS 中的 binary,而 select 是 Power 中的一个 cmdlet shell 因此它在 Windows 的命令行中不起作用(或 Linux 的 bash)。

Furthermore, if you are searching for data from Azure CLI, you should use the --query parameter instead of find

这也是值得商榷的,JMESPath 查询语言过于复杂,没有特别的原因,我不知道 Azure Cli 实际上如何与官方 JMESPath 文档兼容。我更喜欢使用 Powershell 而不是 运行 Azure Cli 命令,并且只使用 Powershell 解析输出 JSON。显然,您可能不像我一样对 Powershell 感到满意,而且您可能觉得这不方便。

我喜欢前面的回答,只是想为企业界被迫选择一个的人补充一个不同的观点:

In that case why do we have 2 separate command sets? Why not just work on Power-Shell?

换句话说:Az CLI 和 Az PowerShell 只调用同一组 API,即 Azure API。 这很重要,因为理论上最终你将能够以两种方式做任何事情

那么为什么微软要创建并维护两种方法来做同样的事情呢?

Martin Fowler 说:但请记住,团队的技能将胜过任何monolith/microservice选择

如果您将 monolith/microservice 更改为 PowerShell/Bash 那么这就回答了问题。

我相信有人使用 Bash 进行了数十年的系统开发,还有其他团队是 PowerShell 的重度用户。 Microsoft 不希望他们必须学习一门全新的编程语言才能使用 Azure。

总结:

如果您的团队熟悉 PowerShell,请使用 PowerShell 并尽可能多地执行原生 PowerShell 操作。通过这种方式,您可以从错误处理、OOP 概念、环境设置、并行化等方面获益

如果您的团队是 linux 管理员,jenkins 的重度用户,有数百万行 Bash 来自动化其他已经存在的事情,并且一生都在与 Bash 打交道,使用 CLI 并保持所有已构建工具的一致性

我在进行 MS Azure 培训(AZ-900 基础知识和 AZ-303 Azure 架构师)时注意到,练习是在 Azure CLI.[=10 中完成的=]

这并不是说它更好,但如果您想参加考试,熟悉它可能是值得的。郑重声明,我是 PowerShell 专家。

Azure Powershell 的另一个问题似乎与 azure cli 不同,那就是版本不同。我花了几天时间弄清楚管道中的哪个脚本的哪个命令的哪个版本需要是什么。真是最可笑的暗杀