您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

安装 Azure Powershell 模块Install the Azure PowerShell module

本文介绍如何使用 PowerShellGet 安装 Azure PowerShell 模块。This article tells you how to install the Azure PowerShell modules using PowerShellGet. 这些说明适用于 Windows、macOS 和 Linux 平台。These instructions work on Windows, macOS, and Linux platforms. 对于 Az 模块,当前不支持其他安装方法。For the Az module, currently no other installation methods are supported.

要求Requirements

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,或者是在 macOS 或 Linux 平台上,请安装最新版 PowerShell CoreIf you aren't sure if you have PowerShell, or are on macOS or Linux, install the latest version of PowerShell Core.

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

$PSVersionTable.PSVersion

若要在 Windows 上的 PowerShell 5.1 中运行 Azure PowerShell,请执行以下操作:To run 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.

在使用 PowerShell Core 时,对于 Azure PowerShell 没有其他要求。There are no additional requirements for Azure PowerShell when using PowerShell Core.

安装 Azure Powershell 模块Install the Azure PowerShell module

警告

对于 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.

建议的安装方法是仅为活跃用户安装:The recommended install method is to only install for the active user:

Install-Module -Name Az -AllowClobber -Scope CurrentUser

如果要在系统上为所有用户进行安装,需要有管理员权限。If you want to install for all users on a system, this requires administrator privileges. 从提升的 PowerShell 会话中,以管理员身份运行,或使用 sudo 命令在 macOS 或 Linux 上运行:From an elevated PowerShell session either run as administrator or with the sudo command on macOS or Linux:

Install-Module -Name Az -AllowClobber -Scope AllUsers

默认情况下,PowerShell 库未配置为 PowerShellGet 的受信任存储库。By default, the PowerShell gallery isn't configured as a trusted repository for PowerShellGet. 首次使用 PSGallery 时会看到以下提示:The first time you use the PSGallery you see the following prompt:

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"):

请回答 YesYes to All 继续安装。Answer Yes or Yes to All to continue with the installation.

Az 模块是 Azure PowerShell cmdlet 的汇总模块。The Az module is a rollup module for the Azure PowerShell cmdlets. 安装它时,系统会下载所有可用的 Azure 资源管理器模块并使其 cmdlet 可供使用。Installing it downloads all of the available Azure Resource Manager modules, and makes their cmdlets available for use.

故障排除Troubleshooting

以下是安装 Azure PowerShell 模块时出现的一些常见问题。Here are some common problems seen when installing the Azure PowerShell module. 如果遇到的问题未在本文中列出,请在 GitHub 上提出问题If you experience a problem not listed here, please file an issue on GitHub.

代理阻止连接Proxy blocks connection

如果从 Install-Module 获得的错误指示 PowerShell 库无法访问,你可能位于某个代理后面。If you get errors from Install-Module that indicate the PowerShell Gallery is unreachable, you may be behind a proxy. 不同的操作系统对于如何配置系统范围的代理会有不同的要求,本文对此不做详述。Different operating systems will have different requirements for configuring a system-wide proxy, which are not covered in detail here. 请联系系统管理员,了解你的代理设置,以及如何针对 OS 来配置它们。Contact your system administrator for your proxy settings and how to configure them for your OS.

PowerShell 本身可能尚未配置为自动使用此代理。PowerShell itself may not be configured to use this proxy automatically. 使用 PowerShell 5.1 及更高版本时,请通过以下命令配置代理,使之能够用于 PowerShell 会话:With PowerShell 5.1 and later, configure the proxy to use for a PowerShell session with the following command:

(New-Object System.Net.WebClient).Proxy.Credentials = `
  [System.Net.CredentialCache]::DefaultNetworkCredentials

如果操作系统凭据已正确配置,则会通过代理路由 PowerShell 请求。If your operating system credentials are configured correctly, this will route PowerShell requests through the proxy. 若要使该设置存在于不同的会话中,请将命令添加到 PowerShell 配置文件In order to have this setting persist between sessions, add the command to a PowerShell profile.

若要安装此包,代理必须允许与以下地址之间的 HTTPS 连接:In order to install the package, your proxy needs to allow HTTPS connections to the following address:

  • https://www.powershellgallery.com

登录Sign in

若要开始使用 Azure PowerShell,请通过 Azure 凭据登录。To start working with Azure PowerShell, sign in with your Azure credentials.

# Connect to Azure with a browser sign in token
Connect-AzAccount

备注

如果已禁用模块自动加载,请使用 Import-Module Az 手动导入模块。If you've disabled module autoloading, manually import the module with Import-Module Az. 考虑到模块的构造方式,这可能需要几秒钟时间。Because of the way the module is structured, this can take a few seconds.

需要在每个新启动的 PowerShell 会话中重复这些步骤。You'll need to repeat these steps for every new PowerShell session you start. 若要了解如何跨 PowerShell 会话保持 Azure 登录状态,请参阅跨 PowerShell 会话保持用户凭据To learn how to persist your Azure sign-in across PowerShell sessions, see Persist user credentials across PowerShell sessions.

更新 Azure PowerShell 模块Update the Azure PowerShell module

Update-Module 命令因 Az 模块的打包方式而无法正确更新你的安装。Because of how the Az module is packaged, the Update-Module command won't update your installation correctly. 当你安装 Az 模块时,它实际上会收集并安装它的所有依赖子模块,这些子模块为每个服务提供 cmdlet。When you install the Az module, it actually collects and installs all of its dependent submodules, and which provide the cmdlets for each service. 这意味着,若要更新 Azure PowerShell 模块,需要__重新安装__,而不是仅仅进行__更新__。That means that to update the Azure PowerShell module, you will need to reinstall, rather than just update. 执行此操作的方式与安装方式相同,但可能需要添加 -Force 参数:This is done in the same way as installing, but you may need to add the -Force argument:

Install-Module -Name Az -AllowClobber -Force

虽然这可能会覆盖已安装的模块,但旧版本仍然可能会保留在系统上。Although this can overwrite installed modules, you may still have older versions left on your system. 若要了解如何从系统中删除旧版 Azure PowerShell,请参阅卸载 Azure PowerShell 模块To learn how to remove old versions of Azure PowerShell from your system, see Uninstall the Azure PowerShell module.

使用多个版本的 Azure PowerShellUse multiple versions of Azure PowerShell

可以安装多个版本的 Azure PowerShell。It's possible to install more than one version of Azure PowerShell. 若要检查是否安装了多个版本的 Azure PowerShell,请使用以下命令:To check if you have multiple versions of Azure PowerShell installed, use the following command:

Get-InstalledModule -Name Az -AllVersions | select Name,Version

若要删除 Azure PowerShell 的某个版本,请参阅卸载 Azure PowerShell 模块To remove a version of Azure PowerShell, see Uninstall the Azure PowerShell module.

可以通过使用 -RequiredVersion 参数来安装或加载 Az 模块的特定版本:You can install or load a specific version of the Az module by using the -RequiredVersion argument:

# Install Az version 0.7.0
Install-Module -Name Az -RequiredVersion 0.7.0 
# Load Az version 0.7.0
Import-Module -Name Az -RequiredVersion 0.7.0

如果安装了该模块的多个版本,则模块自动加载和 Import-Module 默认情况下会加载最新版本。If you have more than one version of the module installed, module autoload and Import-Module load the latest version by default.

提供反馈Provide feedback

如果发现 Azure Powershell 中存在 bug,请在 GitHub 上提出问题If you find a bug in Azure Powershell, file an issue on GitHub. 若要从命令行提供反馈,请使用 Send-Feedback cmdlet。To provide feedback from the command line, use the Send-Feedback cmdlet.

后续步骤Next Steps

若要详细了解 Azure PowerShell 模块及其功能,请参阅 Azure PowerShell 入门To learn more about the Azure PowerShell modules and their features, see Get Started with Azure PowerShell. 如果你熟悉 Azure PowerShell 并且需要从 AzureRM 进行迁移,请参阅从 AzureRM 迁移到 AzIf you're familiar with Azure PowerShell and need to migrate from AzureRM, see Migrate from AzureRM to Az.