练习 - 安装 Azure PowerShell

已完成

在本单元中,你将了解如何检查本地计算机上安装的 PowerShell 版本并安装最新版本。

备注

本练习将指导你如何创建 PowerShell 工具的本地安装。 本模块的其余部分使用 Azure Cloud Shell,以便你可以利用 Microsoft Learn 中的免费订阅支持。 如果愿意,可将此练习视为可选活动,并且仅查看说明。

Linux

安装适用于 Linux 的 PowerShell 涉及使用包管理器。 对于示例我们将使用“Ubuntu 18.04”,但文档中提供其他版本和发行版的详细说明

使用高级打包工具 (apt) 和 Bash 命令行在 Ubuntu Linux 上安装 PowerShell。

  1. 导入 Microsoft Ubuntu 存储库的加密密钥。 该密钥支持包管理器验证安装的 PowerShell 包是否来自 Microsoft。

    curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
    
  2. 注册 Microsoft Ubuntu 存储库,以便包管理器可以找到 PowerShell 包:

    sudo curl -o /etc/apt/sources.list.d/microsoft.list https://packages.microsoft.com/config/ubuntu/18.04/prod.list
    
  3. 更新包列表:

    sudo apt-get update
    
  4. 安装 PowerShell:

    sudo apt-get install -y powershell
    
  5. 启动 PowerShell,验证是否已成功安装:

    pwsh
    

macOS

在 macOS 上,使用 Homebrew 包管理器安装 PowerShell。

重要

如果“brew”命令不可用,可能需要安装 Homebrew 包管理器。 有关详细信息,请转到 Homebrew 网站

  1. 安装 Homebrew-Cask 以获取更多包,包括 PowerShell 包:

    brew install --cask powershell
    
  2. 启动 PowerShell,验证是否已成功安装:

    pwsh
    

Windows

Windows PowerShell 包含在 Windows 操作系统中;但是,我们建议安装 PowerShell 7.0.6 LTS、PowerShell 7.1.3 或更高版本以与 Azure Az PowerShell 模块 PowerShell 一起使用。 可以使用以下步骤检查安装了哪个版本:

  1. 在“系统托盘搜索框”中,输入“PowerShell”。 可能有多个快捷方式链接:

    • PowerShell 7 (x64) - 64 位版本。 通常,应选择此快捷方式。
    • Windows PowerShell - Windows 随附的 64 位版本。
    • Windows PowerShell (x86) - 在 64 位 Windows 上安装的 32 位版本。
    • Windows PowerShell ISE - 集成脚本环境 (ISE) 用于在 Windows PowerShell 中编写脚本。
    • Windows PowerShell ISE (x86) - Windows 上的 ISE 的 32 位版本。
  2. 选择最匹配的 PowerShell 图标。

  3. 键入以下命令,确定安装的 PowerShell 版本。

    $PSVersionTable.PSVersion
    

    or

    pwsh -ver
    

    如果主版本号低于 7,请按照相关说明升级现有的 Windows PowerShell。 此外,请务必安装 SDK 以支持 .NET 工具。

    需要安装 .NET SDK 以运行此命令。

    dotnet tool install --global PowerShell
    

    安装 .NET 工具后,再次运行 PowerShell 版本命令以验证安装是否完成。

还需要设置本地计算机以支持 PowerShell。 在下一个单元中,我们将回顾可添加的命令,包括 Azure Az PowerShell 模块。