你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

安装 Azure PowerShell 服务管理模块

注意

本文档中引用的 cmdlet 用于管理使用服务管理 API 的旧式 Azure 资源。 若要了解用于管理 Azure 资源管理器资源的 cmdlet,请参阅 Az PowerShell 模块

Azure PowerShell 服务管理模块是一个汇总模块。 安装 Azure PowerShell 服务管理模块将下载用于管理使用服务管理 API 的旧 Azure 资源的正式发布的模块,并使它们的 cmdlet 可供使用。

先决条件

重要

Azure PowerShell 服务管理模块仅适用于 Windows PowerShell。 它与 PowerShell 6 或更高版本不兼容,并且不在 Linux 或 macOS 上运行。

  • 从 PowerShell 运行以下命令以确定 PowerShell 版本:

    $PSVersionTable.PSVersion
    
  • 更新到 Windows PowerShell 5.1

  • 将 PowerShell 执行策略设置为“远程签名”或“更低限制”

    • 检查 PowerShell 执行策略:

      Get-ExecutionPolicy -List
      
    • 将 PowerShell 执行策略设置为远程签名:

      Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
      

      若要详细了解执行策略,请参阅 about_Execution_Policies

  • 更新用于 Windows PowerShell 5.1 的 PowerShellGet

安装

使用 Install-Module cmdlet 安装 Azure PowerShell 服务管理模块:

Install-Module -Name Azure, Azure.Storage -Repository PSGallery -AllowClobber -Force

Azure PowerShell 服务管理模块与 Azure PowerShell 资源管理器模块共享依赖项。 如果已安装 Azure PowerShell 资源管理器模块,则需要上一命令中显示的 AllowClobber 参数。 这样,就可更新现有的共享依赖项。 如果不提供此参数,模块安装会失败。

安装 Azure PowerShell 服务管理模块后,请导入模块:

Import-Module -Name Azure

登录

若要开始使用 Azure PowerShell 服务管理模块管理旧 Azure 资源,请启动 PowerShell 会话并运行 Add-AzureAccount 以登录到 Azure:

Add-AzureAccount

登录到 Azure 后,Azure PowerShell 服务管理模块将为给定的会话创建上下文。 该上下文包含用于该会话中所有 cmdlet 的 Azure 环境、帐户、租户和订阅。

疑难解答

如果收到错误“未加载版本为'4.3.0'的指定模块'Azure.Storage',因为未在任何模块目录中找到有效的模块文件。”,则需要安装 Azure.Storage PowerShell 模块

Install-Module -Name Azure.Storage -Repository PSGallery -AllowClobber -Force

如果收到错误“在模块'PowerShellGet'中找到'Install-Module'命令,但无法加载该模块。”,则需要将 PowerShell 执行策略设置为远程签名或限制较少

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

另请参阅

有关 Azure PowerShell 服务管理模块中的命令的详细信息,请参阅 cmdlet 参考文档