为 Azure Stack Hub 安装 PowerShell Az 和 Azure Stack 模块

Azure Stack Hub 版本 AzureStack PowerShell 版本
2102 2.1.1
2108 2.2.0
2206 2.3.0
2301 2.4.0

有关 AzureStack 模块的详细信息,请参阅 PSGallery

本文介绍如何使用 PowerShellGet 安装 Azure PowerShell Az 和兼容的 Azure Stack Hub 管理员模块。 Az 模块可以在 Windows、macOS 和 Linux 平台上安装。

还可以在 Docker 容器中运行 Azure Stack Hub 的 Az 模块。 有关说明,请参阅使用 Docker 运行适用于 Azure Stack Hub 的 PowerShell

如果要安装适用于 Azure Stack Hub 的 PowerShell 资源模块 (AzureRM) 模块,请参阅安装适用于 Azure Stack Hub 的 PowerShell AzureRM 模块

重要

可能不会有新的 Azure 资源模块模块版本。 “Azure 资源模块”模块仅支持关键修补程序。 今后,Azure Stack Hub 将只有 Az 版本。

可以使用 API 配置文件为 Azure Stack Hub 资源提供程序指定兼容的终结点。

API 配置文件提供一种管理 Azure 与 Azure Stack Hub 之间版本差异的方式。 API 版本配置文件是一组具有特定 API 版本的 Azure 资源管理器 PowerShell 模块。 每个云平台都有一组支持的 API 版本配置文件。 例如,Azure Stack Hub 支持特定的配置文件版本,如 2020-09-01-hybrid。 安装配置文件时,会安装与指定的配置文件对应的 Azure 资源管理器 PowerShell 模块。

可在已连接到 Internet、部分联网或离线场景中安装与 Azure Stack Hub 兼容的 PowerShell Az 模块。 本文将引导你完成适用于这些场景的详细说明。

1.验证先决条件

安装了 Update 2002 或更高版本以及最新修补程序的 Azure Stack Hub 支持 Az 模块。 有关详细信息,请参阅 Azure Stack Hub 发行说明

Azure PowerShell Az 模块适用于 Windows 上的 PowerShell 5.1 或更高版本,或者所有平台上的 PowerShell Core 6.x 及更高版本。 你应该安装适用于你的操作系统的最新版本的 PowerShell Core。 在 PowerShell Core 上运行时,Azure PowerShell 没有其他要求。

若要检查 PowerShell 版本,请运行以下命令:

$PSVersionTable.PSVersion

Windows 先决条件

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

  1. 在必要时更新到 Windows PowerShell 5.1。 如果是在 Windows 10 平台上,则已安装 PowerShell 5.1。

  2. 安装 .NET Framework 4.7.2 或更高版本

  3. 确保你具有最新版本的 PowerShellGet。 在提升的提示符下运行以下 cmdlet:

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    
    powershell -noprofile
    $PSVersionTable
    Uninstall-Module PowershellGet -AllVersions -Force -Confirm:$false
    Get-module PowershellGet
    Find-module PowershellGet
    Install-Module PowershellGet -MinimumVersion 2.2.3 -Force
    

2.Linux 和 Mac 先决条件

需要 PowerShell Core 6.x 或更高版本。 请访问链接获取说明

3.卸载 Azure Stack Hub PowerShell 模块的现有版本

在安装所需版本之前,请确保卸载以前安装的任何 Azure Stack Hub Azure 资源管理器或 Az PowerShell 模块。 使用以下两种方法之一卸载模块:

  1. 若要卸载现有的 Azure 资源管理器和 Az PowerShell 模块,请关闭所有活动的 PowerShell 会话,并运行以下 cmdlet:

    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 会话,然后重新运行上述脚本。

  2. 如果 Uninstall-Module 未成功,请从 $env:PSModulePath 位置删除所有以 AzureAzAzs. 开头的文件夹。 对于 Windows PowerShell,位置可能是 C:\Program Files\WindowsPowerShell\ModulesC:\Users\{yourusername}\Documents\WindowsPowerShell\Modules。 对于 PowerShell Core,位置可能是 C:\Program Files\PowerShell\7\ModulesC:\Users\{yourusername}\Documents\PowerShell\Modules。 删除这些文件夹会删除任何现有的 Azure PowerShell 模块。

4.已联网:通过 Internet 连接安装

Azure Stack Az 模块将在 Windows 计算机上与 PowerShell 5.1 或更高版本配合使用,在 Linux 或 macOS 平台上与 PowerShell 6.x 或更高版本配合使用。 使用 PowerShellGet cmdlet 是首选的安装方法。 此方法在受支持平台上的工作原理相同。

  1. 从 PowerShell 会话运行以下命令,以将 PowerShellGet 至少更新到版本 2.2.3

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Install-Module PowerShellGet -MinimumVersion 2.2.3 -Force
    
  2. 关闭 PowerShell 会话,然后打开新的 PowerShell 会话,以使更新生效。

  3. 运行以下命令以安装 Az 模块。

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Install-Module -Name Az.BootStrapper -Force
    Install-AzProfile -Profile 2020-09-01-hybrid -Force
    
  4. 安装 AzureStack PowerShell 模块。

    Install-Module -Name AzureStack -RequiredVersion 2.4.0
    
    Install-Module -Name AzureStack -RequiredVersion 2.3.0
    
    Install-Module -Name AzureStack -RequiredVersion 2.2.0
    
    Install-Module -Name AzureStack -RequiredVersion 2.1.1
    

警告

对于适用于 Windows 的 PowerShell 5.1,不能同时安装 Azure 资源管理器 (AzureRM) 和 Az 模块。 如果需要确保 Azure 资源管理器在系统上始终可用,请为 PowerShell Core 6.x 或更高版本安装 Az 模块。 为此,请安装 PowerShell Core 6.x 或更高版本,然后在 PowerShell Core 终端中按照这些说明操作。

5.离线:不通过 Internet 连接安装

在离线场景中,请先将 PowerShell 模块下载到已建立 Internet 连接的计算机。 然后,将它们传输到要安装的 Azure Stack 开发工具包 (ASDK)。

登录到已建立 Internet 连接的计算机,并根据 Azure Stack Hub 的版本,使用以下脚本下载 Azure 资源管理器和 Azure Stack Hub 包。

安装分为五步:

  1. 将 Azure Stack Hub PowerShell 安装到连接的计算机。
  2. 启用其他存储功能。
  3. 将 PowerShell 包传输到已断开连接的工作站。
  4. 在断开连接的工作站上手动启动 NuGet 提供程序。
  5. 确认已安装 PowerShell。

安装 Azure Stack Hub PowerShell

  1. 可以使用 AzureRMAz 模块。 以下代码保存可信联机存储库 https://www.powershellgallery.com/ 中的 Az 模块。

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    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 2.0.1
    
  2. 安装 Az 模块后,继续安装 AzureStack 模块。

    Save-Package -ProviderName NuGet -Source https://www.powershellgallery.com/api/v2 -Name AzureStack -Path $savedModulesPath -Force -RequiredVersion 2.4.0
    
    Save-Package -ProviderName NuGet -Source https://www.powershellgallery.com/api/v2 -Name AzureStack -Path $savedModulesPath -Force -RequiredVersion 2.3.0
    
    Save-Package -ProviderName NuGet -Source https://www.powershellgallery.com/api/v2 -Name AzureStack -Path $savedModulesPath -Force -RequiredVersion 2.2.0
    
    Save-Package -ProviderName NuGet -Source https://www.powershellgallery.com/api/v2 -Name AzureStack -Path $savedModulesPath -Force -RequiredVersion 2.1.1
    

注意

在没有 Internet 连接的计算机上,建议执行以下 cmdlet 以禁用遥测数据收集功能。 在不禁用遥测数据收集功能的情况下,可能会遇到 cmdlet 性能降级的问题。 这仅适用于没有 Internet 连接的计算机。

Disable-AzDataCollection

将包添加到工作站

  1. 将下载的程序包复制到 USB 设备。

  2. 登录到已断开连接的工作站,将包从 USB 设备复制到工作站中的某个位置。

  3. 在断开连接的工作站上手动启动 NuGet 提供程序。 有关说明,请参阅在未连接到 Internet 的计算机上手动启动 NuGet 提供程序

  4. 将此位置注册为默认存储库,并从此存储库安装 AzureRMAzureStack 模块:

     # requires -Version 5
     # requires -RunAsAdministrator
     # requires -Module PowerShellGet
     # requires -Module PackageManagement
    
     $SourceLocation = "<Location on the development kit that contains the PowerShell packages>"
     $RepoName = "MyNuGetSource"
     [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
     Register-PSRepository -Name $RepoName -SourceLocation $SourceLocation -InstallationPolicy Trusted
    
  5. 安装 Az 模块。

     Install-Module -Name Az -Repository $RepoName -RequiredVersion 2.0.1 -Scope AllUsers
    
  6. 安装 AzureStack 模块。

    Install-Module -Name AzureStack -Repository $RepoName -RequiredVersion 2.4.0 -Scope AllUsers
    
    Install-Module -Name AzureStack -Repository $RepoName -RequiredVersion 2.3.0 -Scope AllUsers
    
    Install-Module -Name AzureStack -Repository $RepoName -RequiredVersion 2.2.0 -Scope AllUsers
    
    Install-Module -Name AzureStack -Repository $RepoName -RequiredVersion 2.1.1 -Scope AllUsers
    

确认已安装 PowerShell

运行以下命令来确认安装:

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

6.配置 PowerShell 以使用代理服务器

在需要代理服务器访问 Internet 的场景中,请先将 PowerShell 配置为使用现有的代理服务器:

  1. 打开提升的 PowerShell 命令提示符。

  2. 运行以下命令:

    #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 模块

可以使用基于 AzureRM 模块的 cmdlet 和代码示例。 但是,你需要更改模块和 cmdlet 的名称。 模块名称已更改,以便 AzureRM 和 Azure 变为 Az,对于 cmdlet 也是如此。 例如,AzureRM.Compute 模块已重命名为 Az.Compute New-AzureRMVM 已变为 New-AzVM,并且 Get-AzureStorageBlob 现在为 Get-AzStorageBlob

有关将 AzurRM 脚本移到 Az 中和 Azure Stack Hub 的 Az 模块中的中断性变更的更详尽讨论和指导,请参阅从 AzureRM 迁移到 Azure PowerShell Az

已知问题

安装 Az 模块时引发错误

  • 适用于:此问题适用于 2002 及更高版本
  • 原因:安装模块时引发错误。 错误消息的开头为:Register-PacakgeSource : A parameter cannot be found that matches parameter name. 'PackageManagementProvider'.,错误消息也可能包含以下文本:PackageManagement\Install-Package : Cannot convert value "2.0.1-preview" to type "System.Version". Error: "Input string was not in a correct format."
  • 补救措施:请在同一会话中运行以下 cmdlet:
    Install-Module PowershellGet -MinimumVersion 2.3.0 -Force
    关闭会话并启动权限提升的新 PowerShell 会话。
  • 发生次数:通用

安装 Az 模块时,错误地引发“需要管理员权限”错误

  • 适用于:此问题适用于 2002 及更高版本
  • 原因:在提升的提示符下安装模块时,会引发错误。 错误指出“Administrator rights required”。
  • 补救措施:关闭会话并启动权限提升的新 PowerShell 会话。 请确保会话中没有加载现有的 Az. Accounts 模块。
  • 发生次数:通用

使用 2020-09-01-hybrid 配置文件时,Cmdlet New-AzVmss 失败

  • 适用:此问题适用于 2020-09-01-hybrid 配置文件。
  • 原因:cmdlet New-AzVmss 不适用于 2020-09-01-hybrid 配置文件。
  • 修正:使用模板创建虚拟机规模集。 可以在 GitHub 存储库 AzureStack-QuickStart-Templates/101-vmss-windows-vm 中找到 Azure Stack Hub 资源管理器模板的示例,还可以找到有关将 Azure Stack Hub 资源管理器和 Visual Studio Code 结合使用的说明。
  • 发生次数:通用

运行 PowerShell 脚本时引发错误

  • 适用于:此问题适用于 2002 及更高版本。

  • 原因:使用特定于 Azure Stack Hub 的模块运行脚本或 PowerShell 命令时,该脚本或命令需要在该模块中可用。 你可能会看到以下错误:

    Method 'get_SerializationSettings' in type 'Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient' from assembly 'Microsoft.Azure.Commands.ResourceManager.Common, Version=4.0.0.0, 
    Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.
    

    当前模块是 PowerShell Az 模块,它取代了 PowerShell AzureRM 模块。 如果在安装 Az 模块后尝试运行某个调用 AzureRM 命令的脚本,该脚本将引发错误。 或者,如果在安装 AzureRM 模块后尝试运行某个调用 Az 命令的脚本,该脚本将引发错误。

  • 修正:卸载 AzureRM 模块并安装 Az 模块。 有关说明,请参阅安装适用于 Azure Stack Hub 的 PowerShell Az 模块。 如果你正在使用 Azure Stack Hub 工具,请使用 Az 工具。 从 az 分支克隆工具存储库,或者从 az 分支下载 AzureStack-Tools 。 有关说明,请参阅从 GitHub 下载 Azure Stack Hub 工具

  • 发生次数:通用

New-AzADServicePrincipal 和 New-AzADApplication 引发的错误

  • 适用:使用 Microsoft Entra ID 的 Azure Stack 环境。

  • 原因:Azure Active Directory Graph 引入了中断性变更,将 Active Directory 应用程序的 IdentifierUri 限制为目录中已验证域的子域。 在更改之前,仅对多租户应用强制实施此限制。 现在,此限制也适用于单租户应用。 此更改将导致以下错误:Values of identifierUris property must use a verified domain of the organization or its subdomain' is displayed when running

  • 补救措施:可以通过两种方式解决此限制。

    • 需要使用作为目录租户子域的服务主体名称。 例如,如果目录为 contoso.partner.onmschina.cn,则服务主体名称必须为 <foo>.contoso.partner.onmschina.cn 形式。 使用以下 cmdlet:

      New-AzADServicePrincipal -Role Owner -DisplayName <foo>.contoso.partner.onmschina.cn
      

      有关标识以及在 Azure Stack Hub 中使用服务主体的详细信息,请参阅 Azure Stack Hub 的标识提供者概述

    • 创建提供有效 IdentifierUri 的 Microsoft Entra 应用,然后使用以下 cmdlet 创建与应用关联的服务主体:

      $app=New-AzADApplication -DisplayName 'newapp' -IdentifierUris http://anything.contoso.partner.onmschina.cn
      New-AzADServicePrincipal -Role Owner -ApplicationId $app.ApplicationId
      
  • 发生次数:通用

错误:“SharedTokenCacheCredential 身份验证失败”

  • 适用于:此问题适用于所有支持的版本。
  • 原因:将多个版本的 AzAccounts 与 Azure Stack Hub PowerShell 模块版本 2.1.1 一起安装时,将引发 SharedTokenCacheCredential 身份验证失败错误。
  • 修正:删除所有版本的 AzAccounts,仅安装受支持的 AzAccounts 版本 2.2.8。
  • 发生次数:通用

后续步骤