简体   繁体   English

Azure CLI 与 Powershell?

[英]Azure CLI vs Powershell?

Not precisely able to understand the merit of Azure CLI on Windows environment.不能完全理解 Azure CLI 在 Windows 环境中的优点。

Is it targetted for the audience who want to manage Azure IAAS from Linux environment?它是否针对想要从 Linux 环境管理 Azure IAAS 的受众?

I thought Powershell core is going to be the way for non-Windows admins.我认为Powershell 内核将成为非 Windows 管理员的方式。 Is PowerShell Core not going to be ported to well on all platforms, to serve the cross-platform audience? PowerShell Core 是否不会在所有平台上很好地移植,以服务于跨平台的受众?

In a nutshell, is it worth learning Azure CLI?简而言之,Azure CLI值得学习吗?

Azure CLI is a PowerShell-like-tool available for all platforms. Azure CLI 是一个类似于 PowerShell 的工具,可用于所有平台。 You can use the same commands no matter what platform you use: Windows, Linux or Mac.无论您使用什么平台,都可以使用相同的命令:Windows、Linux 或 Mac。

Now, there are two version Azure CLI.现在,有两个版本的 Azure CLI。 The Azure CLI 1.0 was written with Node.js to achieve cross-platform capabilities, and the new Azure CLI 2.0 is written in Python to offer better cross-platform capabilities. Azure CLI 1.0 使用 Node.js 编写以实现跨平台功能,而新的 Azure CLI 2.0 使用 Python 编写以提供更好的跨平台功能。 Both are Open Source and available on Github.两者都是开源的,可在 Github 上获得。 However, for now, only certain PowerShell cmdlets support use on Linux.但是,目前,只有某些 PowerShell cmdlet 支持在 Linux 上使用。

Is it targetted for the audience who want to manage Azure IAAS from Linux environment?是否面向想要从 Linux 环境管理 Azure IAAS 的受众?

I think the answer is yes.我认为答案是肯定的。 For a Linux or Mac developer, I think they more likely to use Azure CLI.对于 Linux 或 Mac 开发人员,我认为他们更有可能使用 Azure CLI。

Both, Azure CLI and the PowerShell package use the REST API of Azure. Azure CLI 和 PowerShell 包都使用 Azure 的 REST API。

As one of our Microsoft contacts said: Use whatever you like and you prefer.正如我们的一位 Microsoft 联系人所说:使用您喜欢和喜欢的任何内容。

There are some pros for Azure CLI: Azure CLI 有一些优点:

  1. Open Source - which has many advantages.开源 - 有很多优点。 It might be developing faster in the future.未来可能会发展得更快。 You can view what is really in the Code, the community might or will be bigger, and so on.您可以查看代码中的真正内容,社区可能会或将会更大,等等。
  2. Azure CLI can be used with Groovy, Python, Ruby or any other scripting language. Azure CLI 可与 Groovy、Python、Ruby 或任何其他脚本语言一起使用。 It is easier to get good developers for this languages than an experienced PowerShell developer.与经验丰富的 PowerShell 开发人员相比,获得这种语言的优秀开发人员更容易。 For example: I took Groovy because of Java.例如:因为 Java,我选择了 Groovy。 It is easier to use this combination then learning PowerShell scripting....使用这种组合比学习 PowerShell 脚本更容易......

Currently there is 1 really big positive for PowerShell : there is a huge repository of PowerShell script examples for Azure.目前,PowerShell有一个非常大的好处:Azure 有一个巨大的 PowerShell 脚本示例存储库。 BUT: often you have to do it on your own to fit your requirements - therefore an example might be a good starting point - sometimes it is easier to begin from scratch.但是:通常你必须自己做才能满足你的要求 - 因此一个例子可能是一个很好的起点 - 有时从头开始更容易。

My recommendation would be: Use Azure CLI --> For Scripting use the language your company is familiar with.我的建议是:使用 Azure CLI --> 对于脚本,使用贵公司熟悉的语言。

Just wanted to add my experience with both Azure Cli and Az PowerShell.只是想补充一下我在 Azure Cli 和 Az PowerShell 方面的经验。

As per 2019, both are cross-platform so it should only be a language/syntax preference but with Azure Cli all/most commands are idempotent .根据 2019 年,两者都是跨平台的,因此它应该只是语言/语法首选项,但使用 Azure Cli all/大多数命令都是idempotent

I've ended up replacing all my ARM Templates with Azure Cli scripts as it is less verbose and easy to read.我最终用 Azure Cli 脚本替换了我所有的 ARM 模板,因为它不那么冗长且易于阅读。

With Az Powershell, you still need to check if the resource exists before creating it otherwise it will throw an exception so scripts can become very complicated for no reason.使用 Az Powershell,您仍然需要在创建资源之前检查资源是否存在,否则它会抛出异常,因此脚本可能会无缘无故变得非常复杂。

We're using both Azure Cli and Az Powershell with Azure DevOps.我们将 Azure Cli 和 Az Powershell 与 Azure DevOps 结合使用。 Azure Cli command can now be executed from bash (Linux) or bat (Windows) scripts.现在可以从 bash (Linux) 或 bat (Windows) 脚本执行 Azure Cli 命令。 So it is not optimum... ( EDIT 2020 You can now run Azure CLI using Powershell scripts as well)所以它不是最佳的......(编辑 2020您现在也可以使用 Powershell 脚本运行 Azure CLI)

  • We deploy resources with Azure Cli because it is easy to read and commands are idempotent.我们使用 Azure Cli 部署资源,因为它易于阅读且命令是幂等的。
  • For complex scripting I prefer using PowerShell as there are a lot of useful language features.对于复杂的脚本,我更喜欢使用 PowerShell,因为它有很多有用的语言功能。

EDIT 2021编辑 2021

With the new Azure Bicep , I've started going back to "ARM type" deployment: Bicep is really easy to use if you understand ARM template and stay readable even with complex deployments.使用新的Azure Bicep ,我已经开始回到“ARM 类型”部署:如果您了解 ARM 模板并且即使在复杂的部署中也保持可读性,Bicep 真的很容易使用。

In addition to Az CLI (Powershell core) it handles most of the scripting i need to do.除了 Az CLI(Powershell 核心)之外,它还处理我需要做的大部分脚本。

PowerShell has some significant advantages over CLI: PowerShell 比 CLI 有一些显着的优势:

  • PowerShell is a language of Azure Functions . PowerShell 是 Azure Functions 的一种语言 So you can easily write a module or code and push it to a secured Azure Function.因此,您可以轻松编写模块或代码并将其推送到安全的 Azure 函数。 CLI is not a supported language. CLI 不是受支持的语言。
  • Same goes for Azure Automation Runbooks , whereas CLI is not a supported language. Azure 自动化 Runbooks 也是如此,而 CLI 不是受支持的语言。
  • If you want to orchestrate a hybrid workload covering on premise and cloud, ie deploy a Service to a Windows Server and then deploy an endpoint in Azure for it to interact with, PowerShell can but CLI can't (and I would surprised if it did).如果您想编排覆盖本地和云的混合工作负载,即将服务部署到 Windows Server,然后在 Azure 中部署端点以便与之交互,PowerShell 可以,但 CLI 不能(如果它确实如此,我会感到惊讶) )。
  • If you want to invest in one language that can call legacy Modules for management, PowerShell can but CLI can't.如果您想投资一种可以调用遗留模块进行管理的语言,PowerShell 可以,但 CLI 不能。
  • If you want to create your own DLL in C# or any other language and incorporate that into your scripts.如果您想用 C# 或任何其他语言创建自己的 DLL 并将其合并到您的脚本中。 I've had to reverse engineer DLLs and wrap them in PowerShell to incorporate into orchestrations.我不得不对 DLL 进行逆向工程并将它们包装在 PowerShell 中以合并到业务流程中。
  • I've had significantly more success extending DevOps pipelines with PowerShell than CLI.与 CLI 相比,我在使用 PowerShell 扩展 DevOps 管道方面取得了更大的成功。
  • PowerShell has very good multithreaded workload support for scenarios such as Azure unit and smoke testing and large Azure parallel deployments (beyond DevOps parallel tasks which get clunky). PowerShell 对 Azure 单元和烟雾测试以及大型 Azure 并行部署(除了笨重的 DevOps 并行任务)等场景具有非常好的多线程工作负载支持。 TBH, I am not aware of native multithreading in Azure CLI. TBH,我不知道 Azure CLI 中的本机多线程。
  • I can develop a module, use it in all the above scenarios and then give it to an end client;我可以开发一个模块,在上述所有场景中使用它,然后将其提供给最终客户; TBH, I don't believe I can do that for CLI (create a module, digitally sign it and then nuget feed distro). TBH,我不相信我可以为 CLI 做到这一点(创建一个模块,对其进行数字签名,然后 nuget feed 发行版)。

There is almost nothing I've not scripted in PowerShell, whereas with CLI I can do something but not others.几乎没有我没有在 PowerShell 中编写脚本的内容,而使用 CLI 我可以做一些事情,但不能做其他事情。 My PowerShell skills have kept relevant for over 10 years where I've been doing enterprise SharePoint, Dynamics, now Office and Azure... so I am biased.我的 PowerShell 技能在 10 多年来一直保持相关性,我一直在做企业 SharePoint、Dynamics,现在是 Office 和 Azure……所以我有偏见。

Powershell was not available in non-Windows platform prior to 2018. Staring Jan 2018, Powershell is available in MacOS and Linux platforms.在 2018 年之前,Powershell 在非 Windows 平台上不可用。从 2018 年 1 月开始,Powershell 在 MacOS 和 Linux 平台上可用。

Installation instructions for Powershell on MacOS or Linux is available here 此处提供 MacOS 或 Linux 上的 Powershell 安装说明

PowerShell is Open Source too. PowerShell 也是开源的。 Microsoft have just released PowerShell Azure Az module (Dec 2018), which uses .Net core.微软刚刚发布了 PowerShell Azure Az 模块(2018 年 12 月),它使用 .Net 核心。 So it runs on Linux,Mac OS and Windows.所以它可以在 Linux、Mac OS 和 Windows 上运行。 Azure CLI is still there for Linux developers, if preferred.如果愿意,Azure CLI 仍然可供 Linux 开发人员使用。

In a nutshell, is it worth learning Azure CLI?简而言之,是否值得学习 Azure CLI?

I've noticed when doing the MS Azure training (AZ-900 fundamentals and AZ-303 Azure Architect) is that the exercises are done in Azure CLI .在进行 MS Azure 培训(AZ-900 基础知识和 AZ-303 Azure 架构师)时,我注意到练习是在Azure CLI中完成的。 Granted, they're all "copy and paste this code" but it seems that MS are favouring CLI over PowerShell, at least for training purposes.诚然,他们都是“复制并粘贴此代码”,但似乎 MS 更喜欢 CLI 而不是 PowerShell,至少出于培训目的。

UPDATE: I recently had a lot of trouble with Azure CLI behind a proxy.更新:我最近在代理背后的 Azure CLI 上遇到了很多麻烦。 PowerShell did NOT have any issues. PowerShell 没有任何问题。 As someone used to PowerShell, the CLI is kinda clunky and dumb.作为习惯使用 PowerShell 的人,CLI 有点笨拙和笨拙。 That said, Terraform requires Azure CLI so that may be a good enough reason to consider it.也就是说,Terraform 需要 Azure CLI,因此这可能是考虑它的充分理由。

Not necessary you'll use the Azure CLI on Linux, you also can use it on Windows and it works pretty well =D您不必在 Linux 上使用 Azure CLI,也可以在 Windows 上使用它,而且效果很好 =D

it depends on what you want to do, in my personal opinion.在我个人看来,这取决于你想做什么。 I prefer to work with Azure CLI commands instead of Azure CMDLETS.我更喜欢使用 Azure CLI 命令而不是 Azure CMDLETS。 But I'm still using PowerShell as my programing language for scripts.但我仍然使用 PowerShell 作为我的脚本编程语言。

Some modules on CLI are more "straight" than AZ or AzRM cmdlets. CLI 上的某些模块比 AZ 或 AzRM cmdlet 更“直接”。

Both are preferable because we can use either of them for writing scripts.两者都更可取,因为我们可以使用它们中的任何一个来编写脚本。 In the case of Powershell or Azure CLI, you need to download the software in order to write scripts but Powershell is more preferred as it is an in-built tool by Microsoft.对于 Powershell 或 Azure CLI,您需要下载软件才能编写脚本,但更喜欢 Powershell,因为它是 Microsoft 的内置工具。 But I have also notice that giving commands through Powershell is more complex if we compare it to command prompt.但我也注意到,如果我们将它与命令提示符进行比较,通过 Powershell 发出命令会更加复杂。

there is an article from MS on the topic, with following outtakes: MS 有一篇关于该主题的文章,内容如下:

Feature parity for Azure services doesn't always exist between Azure CLI and Azure PowerShell. Azure CLI 和 Azure PowerShell 之间并不总是存在 Azure 服务的功能奇偶校验。

When picking the right tool, consider your past experience and current work environment.在选择正确的工具时,请考虑您过去的经验和当前的工作环境。

If you work primarily with Windows systems, Azure PowerShell is a natural fit.如果您主要使用 Windows 系统,Azure PowerShell 是自然而然的选择。 Commands follow a verb-noun naming scheme and data is returned as objects.命令遵循动词-名词命名方案,数据作为对象返回。 If you work primarily with Linux systems, Azure CLI feels more natural.如果您主要使用 Linux 系统,Azure CLI 感觉更自然。

Choose the tool that uses your experience and shortens your learning curve.选择能够利用您的经验并缩短学习曲线的工具。

Since this is a frequently asked question in the Community, an article covering the similarities, differences and nuances between the various Azure command-line options was added to the documentation recently: Choose the right Azure command-line tool .由于这是社区中的常见问题,最近在文档中添加了一篇涵盖各种 Azure 命令行选项之间的异同和细微差别的文章: 选择正确的 Azure 命令行工具 This doc should answer most of your questions as well.该文档也应该回答您的大部分问题。

When it comes to managing Azure, you have many options.在管理 Azure 时,您有很多选择。 Although Azure CLI , Azure PowerShell , and Azure Cloud Shell have overlapping functionality, each operates differently, and the language is sometimes confused with the environment.尽管Azure CLIAzure PowerShellAzure Cloud Shell具有重叠的功能,但各自的运作方式不同,而且语言有时会与环境混淆。

Azure CLI vs Azure PowerShell: Azure CLI and Azure PowerShell are command-line tools that enable you to create and manage Azure resources. Azure CLI 与 Azure PowerShell: Azure CLI 和 Azure PowerShell 是命令行工具,可用于创建和管理 Azure 资源。 While both are cross-platform and installable on Windows, macOS, and Linux, Azure CLI runs in Windows PowerShell, Cmd, or Bash and other Unix shells, whereas Azure PowerShell requires Windows PowerShell or PowerShell.虽然两者都是跨平台的并可安装在 Windows、macOS 和 Linux 上,但Azure CLI可在 Windows PowerShell、Cmd 或 Bash 和其他 Unix shell 中运行,而Azure PowerShell需要 Windows PowerShell 或 PowerShell。

Different Shell Environments: Windows PowerShell, PowerShell, Cmd, and Bash are shell environments.不同的 Shell 环境: Windows PowerShell、PowerShell、Cmd 和 Bash 是 Shell 环境。 Your shell environment not only determines which tools you can use but also changes your command-line experience.您的 shell 环境不仅决定了您可以使用哪些工具,而且还会改变您的命令行体验。

Shell Environment | Azure CLI | Azure PowerShell |
------------------|-----------|------------------|
Cmd               | Yes       |                  |
Bash              | Yes       |                  |
Windows PowerShell| Yes       | Yes              |
PowerShell        | Yes       | Yes              |

Using an Azure command-line tool isn't always necessary, but it's a useful skill to have.使用 Azure 命令行工具并不总是必要的,但它是一项有用的技能。 Here are a few key factors to remember when choosing a command-line tool:以下是选择命令行工具时要记住的几个关键因素:

  • When picking the right tool, consider your past experience and current work environment.在选择正确的工具时,请考虑您过去的经验和当前的工作环境。
  • Azure CLI syntax is similar to that of Bash scripting. Azure CLI 语法类似于 Bash 脚本的语法。 If you work primarily with Linux systems, Azure CLI feels more natural.如果您主要使用 Linux 系统,Azure CLI 感觉更自然。
  • Azure PowerShell is a PowerShell module. Azure PowerShell 是一个 PowerShell 模块。 If you work primarily with Windows systems, Azure PowerShell is a natural fit.如果您主要使用 Windows 系统,Azure PowerShell 是自然而然的选择。 Commands follow a verb-noun naming scheme and data is returned as objects.命令遵循动词-名词命名方案,数据作为对象返回。
  • Keep in mind that feature parity for Azure services doesn't always exist between Azure CLI and Azure PowerShell.请记住,Azure CLI 和 Azure PowerShell 之间并不总是存在 Azure 服务的功能奇偶校验。
  • Choose the tool that uses your experience and shortens your learning curve.选择能够利用您的经验并缩短学习曲线的工具。 Use a different tool when it makes sense to do so.在有意义的情况下使用不同的工具。

I've used both Az Powershel and cli, but when docs are ambiguous or commands are not supported, I highly recommend skipping both and going upstream to Azure Rest API .我已经使用了 Az Powershel 和 cli,但是当文档不明确或不支持命令时,我强烈建议您跳过两者并上游到Azure Rest ZDB974238714CA8A1434A7CE1D08

Azure Rest API is generally called by the other commandline tools so it ends up being the most feature complete and generally well documented. Azure Rest API 通常由其他命令行工具调用,因此它最终成为功能最完整且通常有据可查的工具。 It's cross platform too.它也是跨平台的。

The hardest part is getting auth figured out.最难的部分是弄清楚身份验证。 From powershell I usually use a format something like this, but convert to curl+cli or place in a webapp if needed.从 powershell 开始,我通常使用类似这样的格式,但如果需要,转换为 curl+cli 或放置在 webapp 中。 Here is a random example:这是一个随机的例子:

$azContext = Get-AzContext
$subscriptionId = $azContext.Subscription.Id
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
$token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId)
$authHeader = @{
    'Content-Type'='application/json'
    'Authorization'='Bearer ' + $token.AccessToken
}     
  
$uri = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$rg/providers/Microsoft.Web/sites/$site/config/web?api-version=2022-03-01"
$Body = @{      
    "properties" = @{netFrameworkVersion = "v6.0"}
}
$Params = @{
    Uri = $uri
    Headers = $authHeader
    Method = "PUT"
    Body = $Body | ConvertTo-Json -Depth 10
}
$response = Invoke-RestMethod @Params
$response.properties.netFrameworkVersion

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM