Amazon Tools for PowerShell 在 Linux 或 macOS 上安装 - Amazon Tools for PowerShell
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Amazon Tools for PowerShell 在 Linux 或 macOS 上安装

本主题提供有关如何在 Linux 或 macOS Amazon Tools for PowerShell 上安装的说明。

设置概述

要 Amazon Tools for PowerShell 在 Linux 或 macOS 电脑上安装,你可以从两个软件包选项中进行选择:

  • AWS.Tools— 的模块化版本。 Amazon Tools for PowerShell每项 Amazon 服务都由其自己的独立小型模块提供支持,并带有共享的支持模块AWS.Tools.Common

  • AWSPowerShell。 NetCore— 的单一大模块版本。 Amazon Tools for PowerShell这个单一的大型模块支持所有 Amazon 服务。

    注意

    请注意,单个模块可能太大,无法与 Amazon Lambda 函数一起使用。请改用上面显示的模块化版本。

在运行 Linux 或 macOS 的计算机上设置这些事项涉及以下任务,本主题后面将详细介绍:

  1. 在支持的系统上安装 PowerShell 酷睿 6.0 或更高版本。

  2. 安装 PowerShell Core 后, PowerShell 首先pwsh在系统外壳中运行。

  3. 安装AWS.Tools或 AWSPowerShell。 NetCore。

  4. 运行相应的 Import-Module cmdlet 将模块导入到您的 PowerShell会话中。

  5. 运行 In itializ AWSDefaultConfiguration e- cmdlet 以提供您的 Amazon 凭据。

先决条件

要运行 Amazon Tools for PowerShell Core,您的计算机必须运行 PowerShell Core 6.0 或更高版本。

  • 有关支持的 Linux 平台版本列表以及有关如何在基于 Linux 的计算机 PowerShell 上安装最新版本的信息,请参阅 Microsoft 网站 PowerShell 上的在 Linux 上安装。尚未正式支持某些基于 Linux 的操作系统(如 Arch、Kali 和 Raspbian),但提供各种级别的社区支持。

  • 有关支持的 macOS 版本以及如何在 macOS 上安装最新版本的信息,请参阅 PowerShell 微软网站上的在 m PowerShell acOS 上安装。

在 Linux 或 macOS 上安装 AWS.Tools

可以在运行 C PowerShell ore 6.0 或更高版本的计算机 Amazon Tools for PowerShell 上安装模块化版本。有关如何安装 PowerShell Core 的信息,请参阅 PowerShell在 Microsoft PowerShell 网站上安装各种版本的。

您可以通过以下三种方式之一安装 AWS.Tools

  • 使用 AWS.Tools.Installer 模块中的 cmdlet。该模块简化了其他AWS.Tools模块的安装和更新。 AWS.Tools.Installer需要PowerShellGet并自动下载和安装它的更新版本。 AWS.Tools.Installer自动使您的模块版本保持同步。当您安装或更新到一个模块的较新版本时,中的 cmdlet AWS.Tools.Installer 会自动将所有其他AWS.Tools模块更新到相同的版本。

    此方法将在随后的步骤中描述。

  • AWS.Tools.zip 下载模块并将它们提取到其中一个模块目录中。您可以通过输出 $Env:PSModulePath 变量的值来查找模块目录。

  • 使用 Install-Module cmdlet 从 PowerShell 库中安装每个服务模块。

使用该模块AWS.Tools在 Linux 或 macOS 上安装 AWS.Tools.Installer
  1. 通过运行以下命令启动 PowerShell 核心会话。

    $ pwsh
    注意

    我们建议您不要以具有更高权限的管理员 PowerShell 身份运行,除非手头的任务需要这样做。这是因为此操作具有潜在的安全风险,并且不符合最低特权原则。

  2. 要使用 AWS.Tools.Installer 模块安装模块化的 AWS.Tools 程序包,请运行以下命令。

    PS > Install-Module -Name AWS.Tools.Installer Untrusted repository You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): y

    如果您收到关于存储库 “不受信任” 的通知,系统会询问您是否仍要安装。输入y PowerShell 以允许安装模块。为了在不信任存储库的情况下避免出现提示并安装模块,您可以运行以下命令。

    PS > Install-Module -Name AWS.Tools.Installer -Force
  3. 现在,您可以为要使用的每个服务安装模块。例如,以下命令将安装 Amazon EC2 和 Amazon S3 模块。此命令还会安装指定模块工作所需的任何依赖模块。例如,当您安装第一个 AWS.Tools 服务模块时,它还会安装 AWS.Tools.Common。这是所有 Amazon 服务模块都需要的共享模块。它还会删除模块的较早版本,并将其他模块更新到相同的较新版本。

    PS > Install-AWSToolsModule AWS.Tools.EC2,AWS.Tools.S3 -CleanUp Confirm Are you sure you want to perform this action? Performing the operation "Install-AWSToolsModule" on target "AWS Tools version 4.0.0.0". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Installing module AWS.Tools.Common version 4.0.0.0 Installing module AWS.Tools.EC2 version 4.0.0.0 Installing module AWS.Tools.Glacier version 4.0.0.0 Installing module AWS.Tools.S3 version 4.0.0.0 Uninstalling AWS.Tools version 3.3.618.0 Uninstalling module AWS.Tools.Glacier Uninstalling module AWS.Tools.S3 Uninstalling module AWS.Tools.SimpleNotificationService Uninstalling module AWS.Tools.SQS Uninstalling module AWS.Tools.Common
    注意

    Install-AWSToolsModule cmdlet 从名为 PSGalleryPSRepository (https://www.powershellgallery.com/) 下载所有请求的模块,并将存储库视为可信来源。有关此 PSRepository 的更多信息,请使用命令 Get-PSRepository -Name PSGallery

    前面的命令会将模块安装到系统上的默认目录中。实际目录取决于您的操作系统的发行版和版本以及所 PowerShell 安装的版本。例如,如果您在类似 RHEL 的系统上安装了 PowerShell 7,则默认模块很可能位于/opt/microsoft/powershell/7/Modules(或$PSHOME/Modules)中,而用户模块很可能位于。~/.local/share/powershell/Modules有关更多信息,请参阅微软 PowerShell 网站 PowerShell 上的在 Linux 上安装。要查看模块安装位置,请运行以下命令:

    PS > Get-Module -ListAvailable

    要安装其他模块,请使用相应的模块名称运行类似的命令,如图PowerShell 库中所示

安装 AWSPowerShell。 NetCore 在 Linux 或 macOS 上

要升级到较新版本的 AWSPowerShell。 NetCore,请按照中的说明进行操作Amazon Tools for PowerShell 在 Linux 或 macOS 上更新。卸载早期版本的 AWSPowerShell。 NetCore 第一。

你可以安装 AWSPowerShell。 NetCore 用以下两种方式之一:

  • AWSPowerShell.NetCore.zip 下载模块并将其提取到其中一个模块目录中。您可以通过输出 $Env:PSModulePath 变量的值来查找模块目录。

  • 按照以下步骤所述,使用 Install-Module cmdlet 从 PowerShell 库中安装。

要安装 AWSPowerShell。 NetCore 在 Linux 或 macOS 上使用 Install-Module cmdle t

通过运行以下命令启动 PowerShell 核心会话。

$ pwsh
注意

我们建议您不要sudo pwsh以提升的管理员权限开始 PowerShell 运行 PowerShell 。这是因为此操作具有潜在的安全风险,并且不符合最低特权原则。

要安装 AWSPowerShell. NetCore PowerShell 图库中的单模块包,运行以下命令。

PS > Install-Module -Name AWSPowerShell.NetCore Untrusted repository You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): y

如果您收到关于存储库 “不受信任” 的通知,系统会询问您是否仍要安装。输入y PowerShell 以允许安装模块。为了在不信任存储库的情况下避免出现提示,您可以运行以下命令。

PS > Install-Module -Name AWSPowerShell.NetCore -Force

除非您想为计算机的所有用户安装,否则不必以 root 用户身份运行此命令。 Amazon Tools for PowerShell 为此,请在已启动的 PowerShell 会话中运行以下命令sudo pwsh

PS > Install-Module -Scope AllUsers -Name AWSPowerShell.NetCore -Force

脚本执行

Set-ExecutionPolicy 命令在非 Windows 系统上不可用。你可以运行Get-ExecutionPolicy,这表明在非 Windows 系统上运行的 PowerShell Core 中的默认执行策略设置为Unrestricted。有关更多信息,请参阅 Microsoft Technet 网站上的关于执行策略

由于PSModulePath包含 Amazon 模块目录的位置,因此 Get-Module -ListAvailable cmdlet 会显示您安装的模块。

AWS.Tools

PS > Get-Module -ListAvailable Directory: /Users/username/.local/share/powershell/Modules ModuleType Version Name PSEdition ExportedCommands ---------- ------- ---- --------- ---------------- Binary 3.3.563.1 AWS.Tools.Common Desk {Clear-AWSHistory, Set-AWSHistoryConfiguration, Initialize-AWSDefaultConfiguration, Clear-AWSDefaultConfigurat…

AWSPowerShell.NetCore

PS > Get-Module -ListAvailable Directory: /Users/username/.local/share/powershell/Modules ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Binary 3.3.563.1 AWSPowerShell.NetCore

将 PowerShell 控制台配置为使用 Amazon Tools for PowerShell Core (AWSPowerShell. NetCore 只有)

PowerShell 每当你在模块中运行 cmdlet 时,Core 通常都会自动加载模块。但这不适用于 AWSPowerShell。 NetCore 因为它的体积很大。开始运行 AWSPowerShell。 NetCore cmdlet,你必须先运行该命令。Import-Module AWSPowerShell.NetCoreAWS.Tools 模块中的 cmdlet 不需要此操作。

初始化您的 PowerShell 会话

安装完之后,在基于 Linux 或 macOS 的系统 PowerShell 上启动时 Amazon Tools for PowerShell,必须运行 Initialize -来指定AWSDefaultConfiguration要使用的 Amazon 访问密钥。有关 Initialize-AWSDefaultConfiguration 的更多信息,请参阅使用 Amazon 凭证

注意

在的早期(3.3.96.0 之前)版本中,此 cmdlet 被 Amazon Tools for PowerShell命名为。Initialize-AWSDefaults

版本控制

Amazon Amazon Tools for PowerShell 定期发布新版本以支持新的 Amazon 服务和功能。要确定已安装的版本 Amazon Tools for PowerShell ,请运行 Get-AWSPowerShellVersion cmdlet。

PS > Get-AWSPowerShellVersion Tools for PowerShell Version 4.0.123.0 Copyright 2012-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. Amazon Web Services SDK for .NET Core Runtime Version 3.3.103.22 Copyright 2009-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. Release notes: https://github.com/aws/aws-tools-for-powershell/blob/master/CHANGELOG.md This software includes third party software subject to the following copyrights: - Logging from log4net, Apache License [http://logging.apache.org/log4net/license.html]

要查看当前版本工具中支持的 Amazon 服务列表,请将-ListServiceVersionInfo参数添加到 Get-AWSPowerShellVersion cmdlet 中。

要确定您正在运行 PowerShell 的版本,请输入$PSVersionTable以查看$PSVersionTable自动变量的内容。

PS > $PSVersionTable Name Value ---- ----- PSVersion 6.2.2 PSEdition Core GitCommitId 6.2.2 OS Darwin 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 Platform Unix PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…} PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1 WSManStackVersion 3.0

Amazon Tools for PowerShell 在 Linux 或 macOS 上更新

随着更新版本的 Amazon Tools for PowerShell 发布,您应该定期更新在本地运行的版本。

更新模块化模块 AWS.Tools

要将您的AWS.Tools模块更新到最新版本,请运行以下命令:

PS > Update-AWSToolsModule -CleanUp

此命令将更新所有当前安装的 AWS.Tools 模块,对于那些已成功更新的模块,将删除其早期版本。

注意

Update-AWSToolsModule cmdlet 从名为 PSGalleryPSRepository (https://www.powershellgallery.com/) 下载所有模块,并将其视为可信来源。有关此 PSRepository 的更多信息,请使用命令 Get-PSRepository -Name PSGallery

更新 PowerShell 核心工具

运行 Get-AWSPowerShellVersion cmdlet 以确定你正在运行的版本,并将其与PowerShell 图库网站上提供的适用于 Windows PowerShell 的工具版本进行比较。我们建议您每两到三个星期检查一次。只有在更新到支持新命令和 Amazon 服务的版本后,才会提供对新命令和服务的支持。

在安装较新版本的之前 AWSPowerShell。 NetCore,卸载现有模块。在卸载现有软件包之前,请关闭所有打开的 PowerShell 会话。运行以下命令来卸载该程序包。

PS > Uninstall-Module -Name AWSPowerShell.NetCore -AllVersions

在卸载程序包后,通过运行以下命令来安装更新的模块。

PS > Install-Module -Name AWSPowerShell.NetCore

安装完成后,运行命令将更新后的 cmdlet 加载Import-Module AWSPowerShell.NetCore到您的 PowerShell 会话中。

相关信息