安装适用于 Azure Stack Hub 的 PowerShell Az 预览版模块Install PowerShell Az preview module for Azure Stack Hub

本文介绍如何使用 PowerShellGet 安装 Azure PowerShell Az 和兼容的 Azure Stack Hub 管理员模块。This article explains how to install the Azure PowerShell Az and compatible Azure Stack Hub administrator modules using PowerShellGet. Az 模块可以在 Windows、macOS 和 Linux 平台上安装。The Az modules can be installed on Windows, macOS, and Linux platforms.

还可以在 Docker 容器中运行 Azure Stack Hub 的 Az 模块。You can also run the Az modules for Azure Stack Hub in a Docker container. 有关说明,请参阅使用 Docker 运行适用于 Azure Stack Hub 的 PowerShellFor instructions, see Use Docker to run PowerShell for Azure Stack Hub.

如果要安装适用于 Azure Stack Hub 的 PowerShell AzureRM 模块,请参阅安装适用于 Azure Stack Hub 的 PowerShell AzureRM 模块If you would like to install PowerShell AzureRM module for Azure Stack Hub, see Install PowerShell AzureRM module for Azure Stack Hub.

重要

PowerShell Az 模块目前为公共预览版。The PowerShell Az module is currently in public preview. 此预览版本可能会在即将发布的版本中推出中断性变更。可能不会有新的 AzureRM 模块版本。This preview version may have breaking changes in the upcoming releases There will likely not be new AzureRM module releases. AzureRM 模块仅受关键修补程序支持。The AzureRM modules are under support for critical fixes only. 今后,Azure Stack 只会提供 Az 版本Going forward there will only be Az releases for Azurestack

可以使用 API 配置文件为 Azure Stack Hub 资源提供程序指定兼容的终结点。You can use API profiles to specify the compatible endpoints for the Azure Stack Hub resource providers.

API 配置文件提供一种管理 Azure 与 Azure Stack Hub 之间版本差异的方式。API profiles provide a way to manage version differences between Azure and Azure Stack Hub. 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 Hub 支持特定的配置文件版本,例如 2019-03-01-hybridFor example, Azure Stack Hub 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 Hub 兼容的 PowerShell Az 模块。You can install Azure Stack Hub compatible PowerShell Az 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

带 Update 2002 的 Azure Stack Hub 和最新修补程序支持 Az 模块。Az modules are supported only on Azure Stack Hub with Update 2002 and the latest hotfix.

Azure PowerShell 适用于 Windows 上的 PowerShell 5.1 或更高版本,或者所有平台上的 PowerShell Core 6.x 及更高版本。Azure PowerShell works with PowerShell 5.1 or higher on Windows, or PowerShell Core 6.x and later on all platforms. 你应该安装适用于你的操作系统的最新版本的 PowerShell CoreYou should install the latest version of PowerShell Core available for your operating system. 在 PowerShell Core 上运行时,Azure PowerShell 没有额外要求。Azure PowerShell has no additional requirements when run on PowerShell Core.

若要检查 PowerShell 版本,请运行以下命令:To check your PowerShell version, run the command:

$PSVersionTable.PSVersion

Windows 先决条件Prerequisites for Windows

若要在 Windows 上的 PowerShell 5.1 中使用 Azure PowerShell,请执行以下操作:To use Azure PowerShell in PowerShell 5.1 on Windows:

  1. 在必要时更新到 Windows PowerShell 5.1Update to Windows PowerShell 5.1 if needed. 如果是在 Windows 10 平台上,则已安装 PowerShell 5.1。If you're on Windows 10, you already have PowerShell 5.1 installed.
  2. 安装 .NET Framework 4.7.2 或更高版本Install .NET Framework 4.7.2 or later.
  3. 确保你具有最新版本的 PowerShellGet。Make sure you have the latest version of PowerShellGet. 运行 Install-Module PowerShellGet -MinimumVersion 2.2.3 -ForceRun Install-Module PowerShellGet -MinimumVersion 2.2.3 -Force.

2.Linux 和 Mac 先决条件2. Prerequisites for Linux and Mac

需要 PowerShell Core 6.x 或更高版本。PowerShell Core 6.x or later version is needed. 请访问链接获取说明Follow the link for instructions

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

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

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

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

    如果遇到任何错误(例如“模块已在使用中”),请关闭正在使用模块的 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 文件夹中删除以 AzureAzAzs. 开头的所有文件夹。Delete all the folders that start with Azure, Az 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 连接安装4. Connected: Install with internet connectivity

Azure Stack Az 模块将在 Azure Stack Hub 2002 或更高版本上运行。The Azure Stack Az module will work Azure Stack Hub 2002 or later. 此外,Azure Stack Az 模块将在 Windows 计算机上与 PowerShell 5.1 或更高版本配合使用,在 Linux 或 macOS 平台上与 PowerShell 6.x 或更高版本配合使用。In addition, the Azure Stack Az module will work with PowerShell 5.1 or greater on a Windows machine, or PowerShell 6.x or greater on a Linux or macOS platform. 使用 PowerShellGet cmdlet 是首选的安装方法。Using the PowerShellGet cmdlets is the preferred installation method. 此方法在受支持平台上的工作原理相同。This method works the same on the supported platforms.

从 PowerShell 会话运行以下命令:Run the following command from a PowerShell session:

Install-Module -Name Az.BootStrapper -Force -AllowPrerelease
Install-AzProfile -Profile 2019-03-01-hybrid -Force
Install-Module -Name AzureStack -RequiredVersion 2.0.2-preview -AllowPrerelease

备注

Azure Stack Hub 模块版本 2.0.0 是一项中断性变更。Azure Stack Hub module version 2.0.0 is a breaking change. 有关详细信息,请参阅在 Azure Stack Hub 中从 AzureRM 迁移到 Azure PowerShell AzRefer to the Migrate from AzureRM to Azure PowerShell Az in Azure Stack Hub for details.

警告

对于适用于 Windows 的 PowerShell 5.1,不能同时安装 AzureRM 和 Az 模块。You can't have both the AzureRM and Az modules installed for PowerShell 5.1 for Windows at the same time. 如果需要确保 AzureRM 在系统上可用,请为 PowerShell Core 6.x 或更高版本安装 Az 模块。If you need to keep AzureRM available on your system, install the Az module for PowerShell Core 6.x or later. 为此,请安装 PowerShell Core 6.x 或更高版本,然后在 PowerShell Core 终端中按照这些说明操作。To do this, install PowerShell Core 6.x or later and then follow these instructions in a PowerShell Core terminal.

5.离线:不通过 Internet 连接安装5. Disconnected: Install without 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 Hub 的版本,使用以下脚本下载 Azure 资源管理器和 Azure Stack Hub 包。Sign in to a computer with internet connectivity and use the following scripts to download the Azure Resource Manager and Azure Stack Hub packages, depending on your version of Azure Stack Hub.

安装分为五步:Installation has five steps:

  1. 将 Azure Stack Hub PowerShell 安装到连接的计算机。Install Azure Stack Hub 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 Hub PowerShellInstall Azure Stack Hub PowerShell

Azure Stack Hub 2002 或更高版本。Azure Stack Hub 2002 or later.

可以使用 AzureRM 或 Az 预览版模块。You could either use AzureRM or Az preview modules. 对于 RM 模块,请参阅安装 PowerShell AzureRM 模块中的说明。For RM modules, see the instructions at Install PowerShell AzureRM module. 以下代码保存可信联机存储库 https://www.powershellgallery.com/ 中的模块。The following code saves modules from trustworthy online repository https://www.powershellgallery.com/.


Install-module -Name PowerShellGet -MinimumVersion 2.2.3 -Force
Import-Module -Name PackageManagement -ErrorAction Stop

$savedModulesPath = "<Path that is used to save the packages>"
Save-Package -ProviderName NuGet -Source https://www.powershellgallery.com/api/v2 -Name Az -Path $savedModulesPath -Force -RequiredVersion 0.10.0-preview
Save-Package -ProviderName NuGet -Source https://www.powershellgallery.com/api/v2 -Name AzureStack -Path $savedModulesPath -Force -RequiredVersion 2.0.1-preview

备注

在没有 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-AzDataCollection

将包添加到工作站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 AzureStack -Repository $RepoName -RequiredVersion 2.0.2-preview -AllowPrerelease -Scope AllUsers
    
    Install-Module -Name Az -Repository $RepoName -RequiredVersion 0.10.0-preview -AllowPrerelease -Scope AllUsers
    

确认已安装 PowerShellConfirm the installation of PowerShell

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

Get-Module -Name "Az*" -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
    

7.使用 Az 模块7. Use the Az module

可以使用基于 AzureRM 的 cmdlet 和代码示例。You can use the cmdlets and code samples based on AzureRM. 但是,你需要更改模块和 cmdlet 的名称。However, you will want to change the name of the modules and cmdlets. 模块名称已更改,以便 AzureRM 和 Azure 变为 Az,对于 cmdlet 也是如此。The module names have been changed so that AzureRM and Azure become Az, and the same for cmdlets. 例如,AzureRM.Compute 模块已重命名为 Az.Compute. New-AzureRMVMFor example, the AzureRM.Compute module has been renamed to Az.Compute. New-AzureRMVM 已变为 New-AzVM,并且 Get-AzureStorageBlob 现在为 Get-AzStorageBlobhas become New-AzVM, and Get-AzureStorageBlob is now Get-AzStorageBlob.

有关将 AzurRM 脚本移到 Az 中和 Azure Stack Hub 的 Az 模块中的中断性变更的更详尽讨论和指导,请参阅从 AzureRM 迁移到 Azure PowerShell AzFor a more thorough discussion and guidance for moving AzurRM script to Az and breaking changes in Azure Stack Hub's Az module, see Migrate from AzureRM to Azure PowerShell Az.

用于 Azure Stack Hub 内容的 PowerShell 代码片段使用 AzureRM 模块。The PowerShell snippets used in the Azure Stack Hub content use the AzureRM module. Az 模块为预览版本。The Az module is a preview release. 你可以按照迁移指南在 Azure Stack Hub 中从 AzureRM 迁移到 Azure PowerShell Az 中的指导来重构与 Az 模块配合使用的代码片段。You can refactor the snippets to use with the Az module by following the guidance in the migration guide, Migrate from AzureRM to Azure PowerShell Az in Azure Stack Hub.

后续步骤Next steps