安装适用于 Azure Stack 的 PowerShellInstall PowerShell for Azure Stack

适用于:Azure Stack 集成系统和 Azure Stack 开发工具包Applies to: Azure Stack integrated systems and Azure Stack Development Kit

Azure PowerShell 提供了一组使用 Azure 资源管理器模型管理 Azure Stack 资源的 cmdlet。Azure PowerShell provides a set of cmdlets that use the Azure Resource Manager model for managing your Azure Stack resources.

要使用云,必须安装与 Azure Stack 兼容的 PowerShell 模块。To work with your cloud, you must install Azure Stack compatible PowerShell modules. Azure Stack 使用 AzureRM 模块,而不是全球 Azure 中使用的新型 AzureAZ 模块。Azure Stack uses the AzureRM module rather than the newer AzureAZ module used in global Azure. 你还需要使用 API 配置文件 为 Azure Stack 资源提供程序指定兼容的终结点。You also need to use API profiles to specify the compatible endpoints for the Azure Stack resource providers.

API 配置文件提供一种管理 Azure 与 Azure Stack 之间版本差异的方式。API profiles provide a way to manage version differences between Azure and Azure Stack. API 版本配置文件是一组具有特定 API 版本的 Azure 资源管理器 PowerShell 模块。An API version profile is a set of Azure Resource Manager PowerShell modules with specific API versions. 每个云平台都有一组支持的 API 版本配置文件。Each cloud platform has a set of supported API version profiles. 例如,Azure Stack 支持特定的配置文件版本,例如 2019-03-01-hybridFor example, Azure Stack supports a specific profile version such as 2019-03-01-hybrid. 安装配置文件时,会安装与指定的配置文件对应的 Azure 资源管理器 PowerShell 模块。When you install a profile, the Azure Resource Manager PowerShell modules that correspond to the specified profile are installed.

可在已连接到 Internet、部分联网或离线场景中安装与 Azure Stack 兼容的 PowerShell 模块。You can install Azure Stack compatible PowerShell modules in internet-connected, partially connected, or disconnected scenarios. 本文将引导你完成适用于这些场景的详细说明。This article walks you through the detailed instructions for these scenarios.

1.验证先决条件1. Verify your prerequisites

开始使用 Azure Stack 和 PowerShell 之前,必须具备以下先决条件:Before you get started with Azure Stack and PowerShell, you must have the following prerequisites:

  • PowerShell 版本 5.0PowerShell Version 5.0
    若要检查版本,请运行 $PSVersionTable.PSVersion 并比较版本。To check your version, run $PSVersionTable.PSVersion and compare the Major version. 如果没有 PowerShell 5.0,请按照安装 Windows PowerShell 中所述进行操作。If you don't have PowerShell 5.0, follow the Installing Windows PowerShell.

    Note

    PowerShell 5.0 需要 Windows 计算机。PowerShell 5.0 requires a Windows machine.

  • 在权限提升的命令提示符下运行 PowershellRun Powershell in an elevated command prompt.

  • PowerShell 库访问权限PowerShell Gallery access
    你需要有权访问 PowerShell 库You need access to the PowerShell Gallery. 该库是 PowerShell 内容的中心存储库。The gallery is the central repository for PowerShell content. PowerShellGet 模块包含用于发现、安装、更新和发布 PowerShell 项目的 cmdlet。The PowerShellGet module contains cmdlets for discovering, installing, updating, and publishing PowerShell artifacts. 这些项目的示例包括来自 PowerShell 库和其他专用存储库的模块、DSC 资源、角色功能和脚本。Examples of these artifacts are modules, DSC resources, role capabilities, and scripts from the PowerShell Gallery and other private repositories. 如果在离线场景中使用 PowerShell,则必须从已建立 Internet 连接的计算机检索资源,并将其存储在离线计算机可访问的位置。If you're using PowerShell in a disconnected scenario, you must retrieve resources from a machine with a connection to the internet and store them in a location accessible to your disconnected machine.

验证 PSGallery 是否已注册为存储库。Validate if PSGallery is registered as a repository.

Note

此步骤需要访问 Internet。This step requires internet access.

打开权限提升的 PowerShell 提示符,并运行以下 cmdlet:Open an elevated PowerShell prompt, and run the following cmdlets:

Import-Module -Name PowerShellGet -ErrorAction Stop
Import-Module -Name PackageManagement -ErrorAction Stop
Get-PSRepository -Name "PSGallery"

如果未注册存储库,请打开权限提升的 PowerShell 会话并运行以下命令:If the repository isn't registered, open an elevated PowerShell session and run the following command:

Register-PSRepository -Default
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted

3.卸载 Azure Stack PowerShell 模块的现有版本3. Uninstall existing versions of the Azure Stack PowerShell modules

在安装所需版本之前,请确保卸载以前安装的任何 Azure Stack AzureRM PowerShell 模块。Before installing the required version, make sure that you uninstall any previously installed Azure Stack AzureRM PowerShell modules. 使用以下两种方法之一卸载模块:Uninstall the modules by using one of the following two methods:

  1. 若要卸载现有的 AzureRM PowerShell 模块,请关闭所有活动的 PowerShell 会话,并运行以下 cmdlet:To uninstall the existing AzureRM PowerShell modules, close all the active PowerShell sessions, and run the following cmdlets:

    Get-Module -Name Azs.* -ListAvailable | Uninstall-Module -Force -Verbose
    Get-Module -Name Azure* -ListAvailable | Uninstall-Module -Force -Verbose
    

    如果遇到任何错误(例如“模块已在使用中”),请关闭正在使用模块的 PowerShell 会话,然后重新运行上述脚本。If you hit an error such as 'The module is already in use', close the PowerShell sessions that are using the modules and rerun the above script.

  2. C:\Program Files\WindowsPowerShell\ModulesC:\Users\{yourusername}\Documents\WindowsPowerShell\Modules 文件夹中删除以 AzureAzs. 开头的所有文件夹。Delete all the folders that start with Azure or Azs. from the C:\Program Files\WindowsPowerShell\Modules and C:\Users\{yourusername}\Documents\WindowsPowerShell\Modules folders. 删除这些文件夹会删除任何现有的 PowerShell 模块。Deleting these folders removes any existing PowerShell modules.

4.已联网:在已建立 Internet 连接的情况下安装适用于 Azure Stack 的 PowerShell4. Connected: Install PowerShell for Azure Stack with internet connectivity

需要的 API 版本配置文件和 Azure Stack PowerShell 模块将取决于所运行的 Azure Stack 版本。The API version profile and Azure Stack PowerShell modules you require will depend on the version of Azure Stack you're running.

安装 Azure Stack PowerShellInstall Azure Stack PowerShell

运行以下 PowerShell 脚本,在开发工作站上安装这些模块:Run the following PowerShell script to install these modules on your development workstation:

  • 对于 Azure Stack 1904 或更高版本:For Azure Stack 1904 or later:

    # Install the AzureRM.BootStrapper module. Select Yes when prompted to install NuGet
    Install-Module -Name AzureRM.BootStrapper
    
    # Install and import the API Version Profile required by Azure Stack into the current PowerShell session.
    Use-AzureRmProfile -Profile 2019-03-01-hybrid -Force
    Install-Module -Name AzureStack -RequiredVersion 1.7.2
    
  • 对于 Azure Stack 版本 1903 或更低版本,仅安装以下两个模块:For Azure Stack version 1903 or earlier, only install the two modules below:

    # Install and import the API Version Profile required by Azure Stack into the current PowerShell session.
    
    Install-Module -Name AzureRM -RequiredVersion 2.4.0
    Install-Module -Name AzureStack -RequiredVersion 1.7.1
    

    Note

    • Azure Stack 模块版本 1.7.1 是一个包含中断性变更的版本。The Azure Stack module version 1.7.1 is a breaking change release. 若要从 Azure Stack 1.6.0 迁移,请参阅迁移指南To migrate from Azure Stack 1.6.0, please refer to the migration guide.
    • AzureRM 模块版本 2.4.0 包含对 cmdlet Remove-AzureRmStorageAccount 的中断性变更。The AzureRM module version 2.4.0 comes with a breaking change for the cmdlet Remove-AzureRmStorageAccount. 此 cmdlet 需要指定 -Force 参数,这样就可以在不确认的情况下删除存储帐户。This cmdlet expects -Force parameter to be specified for removing the storage account without confirmation.
    • 无需安装 AzureRM.BootStrapper 即可安装 Azure Stack 版本 1901 或更高版本的模块。You don't need to install AzureRM.BootStrapper to install the modules for Azure Stack version 1901 or later.
    • 如果在 Azure Stack 版本 1901 或更高版本上使用以上 AzureRM 模块,请勿安装 2018-03-01-hybrid 配置文件。Don't install the 2018-03-01-hybrid profile in addition to using the above AzureRM modules on Azure Stack version 1901 or later.

确认已安装 PowerShellConfirm the installation of PowerShell

运行以下命令来确认安装:Confirm the installation by running the following command:

Get-Module -Name "Azure*" -ListAvailable
Get-Module -Name "Azs*" -ListAvailable

如果安装成功,输出中会显示 AzureRM 和 AzureStack 模块。If the installation is successful, the AzureRM and AzureStack modules are displayed in the output.

5.离线:在未建立 Internet 连接的情况下安装 PowerShell5. Disconnected: Install PowerShell without an internet connection

在离线场景中,请先将 PowerShell 模块下载到已建立 Internet 连接的计算机。In a disconnected scenario, you first download the PowerShell modules to a machine that has internet connectivity. 然后,将它们传输到要安装的 Azure Stack 开发工具包 (ASDK)。Then, you transfer them to the Azure Stack Development Kit (ASDK) for installation.

登录到已建立 Internet 连接的计算机,并根据 Azure Stack 的版本,使用以下脚本下载 Azure 资源管理器和 Azure Stack 程序包。Sign in to a computer with internet connectivity and use the following scripts to download the Azure Resource Manager and Azure Stack packages, depending on your version of Azure Stack.

安装分为四步:Installation has four steps:

  1. 将 Azure Stack PowerShell 安装到连接的计算机。Install Azure Stack PowerShell to a connected machine.
  2. 启用其他存储功能。Enable additional storage features.
  3. 将 PowerShell 包传输到已断开连接的工作站。Transport the PowerShell packages to your disconnected workstation.
  4. 在断开连接的工作站上手动启动 NuGet 提供程序。Manually bootstrap the NuGet provider on your disconnected workstation.
  5. 确认已安装 PowerShell。Confirm the installation of PowerShell.

安装 Azure Stack PowerShellInstall Azure Stack PowerShell

  • Azure Stack 1904 或更高版本。Azure Stack 1904 or later.

    Import-Module -Name PowerShellGet -ErrorAction Stop
    Import-Module -Name PackageManagement -ErrorAction Stop
    
    $Path = "<Path that is used to save the packages>"
    Save-Package -ProviderName NuGet -Source https://www.powershellgallery.com/api/v2 -Name AzureRM -Path $Path -Force -RequiredVersion 2.5.0
    Save-Package -ProviderName NuGet -Source https://www.powershellgallery.com/api/v2 -Name AzureStack -Path $Path -Force -RequiredVersion 1.7.2
    
  • Azure Stack 1903 或更低版本。Azure Stack 1903 or earlier.

    Import-Module -Name PowerShellGet -ErrorAction Stop
    Import-Module -Name PackageManagement -ErrorAction Stop
    
    $Path = "<Path that is used to save the packages>"
    Save-Package -ProviderName NuGet -Source https://www.powershellgallery.com/api/v2 -Name AzureRM -Path $Path -Force -RequiredVersion 2.4.0
    Save-Package -ProviderName NuGet -Source https://www.powershellgallery.com/api/v2 -Name AzureStack -Path $Path -Force -RequiredVersion 1.7.1
    

    Note

    Azure Stack 模块版本 1.7.1 是一项中断性变更。The Azure Stack module version 1.7.1 is a breaking change. 若要从 Azure Stack 1.6.0 迁移,请参阅迁移指南To migrate from AzureStack 1.6.0 please refer to the migration guide.

    Note

    在没有 Internet 连接的计算机上,建议执行以下 cmdlet 以禁用遥测数据收集功能。On machines without an internet connection, we recommend executing the following cmdlet for disabling the telemetry data collection. 在不禁用遥测数据收集功能的情况下,可能会遇到 cmdlet 性能降级的问题。You may experience a performance degradation of the cmdlets without disabling the telemetry data collection. 这仅适用于没有 Internet 连接的计算机。This is applicable only for the machines without internet connections

    Disable-AzureRmDataCollection
    

将包添加到工作站Add your packages to your workstation

  1. 将下载的程序包复制到 USB 设备。Copy the downloaded packages to a USB device.

  2. 登录到已断开连接的工作站,将包从 USB 设备复制到工作站中的某个位置。Sign in to the disconnected workstation and copy the packages from the USB device to a location on the workstation.

  3. 在断开连接的工作站上手动启动 NuGet 提供程序。Manually bootstrap the NuGet provider on your disconnected workstation. 有关说明,请参阅在未连接到 Internet 的计算机上手动启动 NuGet 提供程序For instructions, see Manually bootstrapping the NuGet provider on a machine that isn't connected to the internet.

  4. 将此位置注册为默认存储库,并从此存储库安装 AzureRM 和 AzureStack 模块:Register this location as the default repository and install the AzureRM and AzureStack modules from this repository:

    # requires -Version 5
    # requires -RunAsAdministrator
    # requires -Module PowerShellGet
    # requires -Module PackageManagement
    
    $SourceLocation = "<Location on the development kit that contains the PowerShell packages>"
    $RepoName = "MyNuGetSource"
    
    Register-PSRepository -Name $RepoName -SourceLocation $SourceLocation -InstallationPolicy Trusted
    
    Install-Module -Name AzureRM -Repository $RepoName
    
    Install-Module -Name AzureStack -Repository $RepoName
    

确认已安装 PowerShellConfirm the installation of PowerShell

运行以下命令来确认安装:Confirm the installation by running the following command:

Get-Module -Name "Azure*" -ListAvailable
Get-Module -Name "Azs*" -ListAvailable

6.配置 PowerShell 以使用代理服务器6. Configure PowerShell to use a proxy server

在需要代理服务器访问 Internet 的场景中,请先将 PowerShell 配置为使用现有的代理服务器:In scenarios that require a proxy server to access the internet, you first configure PowerShell to use an existing proxy server:

  1. 打开提升的 PowerShell 命令提示符。Open an elevated PowerShell prompt.

  2. 运行以下命令:Run the following commands:

    #To use Windows credentials for proxy authentication
    [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials
    
    #Alternatively, to prompt for separate credentials that can be used for #proxy authentication
    [System.Net.WebRequest]::DefaultWebProxy.Credentials = Get-Credential
    

后续步骤Next steps