你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
本文介绍如何使用 PowerShellGet 安装 Azure PowerShell 模块。 这些说明适用于 Windows、Linux 和 macOS 平台。
Azure PowerShell 也可在 Azure Cloud Shell 中使用。
要求
Azure PowerShell 适用于所有平台上的 PowerShell 5.1 或更高版本,或者所有平台上的 PowerShell Core 6.x 及更高版本。 应安装适用于您操作系统的 PowerShell 的最新版本。 在 PowerShell 6.2.4 及更高版本上运行时,Azure PowerShell 没有其他要求。
若要检查 PowerShell 版本,请运行以下命令:
$PSVersionTable.PSVersion
在 Windows 上的 PowerShell 5.1 中使用 Azure PowerShell:
- 更新到 Windows PowerShell 5.1。 如果是在 Windows 10 版本 1607 或更高版本上,则已安装 PowerShell 5.1。
- 安装 .NET Framework 4.7.2 或更高版本。
- 请确保具有最新版本的 PowerShellGet。 运行
Install-Module -Name PowerShellGet -Force。
安装 Azure PowerShell 模块
警告
我们不支持同时为 Windows 上的 PowerShell 5.1 安装 AzureRM 和 Az 模块。 如果需要使 AzureRM 在系统上可用,请安装适用于 PowerShell 6.2.4 或更高版本的 Az 模块。
使用 PowerShellGet cmdlet 是首选安装方法。 仅为当前用户安装 Az 模块。 这是建议的安装范围。 此方法在 Windows、Linux 和 macOS 平台上的工作方式相同。 从 PowerShell 会话运行以下命令:
if (Get-Module -Name AzureRM -ListAvailable) {
Write-Warning -Message ('Az module not installed. Having both the AzureRM and ' +
'Az modules installed at the same time is not supported.')
} else {
Install-Module -Name Az -AllowClobber -Scope CurrentUser
}
默认情况下,PowerShell 库未配置为 PowerShellGet 的受信任存储库。 首次使用 PSGallery 时,会看到以下提示:
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"):
请回答 Yes 或 Yes to All 继续安装。
在系统上为所有用户安装模块需要提升的权限。 在 Windows 中使用sudo或使用 macOS 或 Linux 上的命令启动 PowerShell 会话:
if (Get-Module -Name AzureRM -ListAvailable) {
Write-Warning -Message ('Az module not installed. Having both the AzureRM and ' +
'Az modules installed at the same time is not supported.')
} else {
Install-Module -Name Az -AllowClobber -Scope AllUsers
}
Az 模块是 Azure PowerShell cmdlet 的汇总模块。 安装它下载所有正式版 Az PowerShell 模块,并使其 cmdlet 可供使用。
脱机安装
在某些环境中,无法连接到PowerShell 库。 在这些情况下,仍可以使用以下方法之一脱机安装:
将模块下载到网络中的另一个位置,并将其用作安装源。 此方法允许在单个服务器或文件共享上缓存 PowerShell 模块,以便将 PowerShellGet 部署到任何断开连接的系统。 了解如何使用 本地 PowerShellGet 存储库来设置本地存储库,并在断开连接的系统上进行安装。
将 Azure PowerShell MSI 下载 到连接到网络的计算机,然后将安装程序复制到系统,而无需访问 PowerShell 库。 请记住,MSI 安装程序仅适用于 Windows 上的 PowerShell 5.1。
将包含 Save-Module 的模块保存到文件共享,或将其保存到另一个源,并手动将其复制到其他计算机:
Save-Module -Name Az -Path '\\server\share\PowerShell\modules' -Force
故障排除
下面是安装 Azure PowerShell 模块时出现的一些常见问题。 如果遇到此处未列出的问题, 请将问题提交到 GitHub 上。
代理阻止连接
如果从 Install-Module 获得的错误指示 PowerShell 库无法访问,你可能位于某个代理后面。 不同的操作系统和网络环境对于如何配置系统范围的代理有不同的要求。 请联系系统管理员,了解你的代理设置,以及如何针对你的环境来配置它们。
PowerShell 本身可能未配置为自动使用此代理。 使用 PowerShell 5.1 及更高版本时,请使用以下命令将 PowerShell 会话配置为使用代理:
$webClient = New-Object System.Net.WebClient
$webClient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
如果操作系统凭据已正确配置,则此配置会通过代理来路由 PowerShell 请求。 若要使此设置在各个会话之间持久存在,请将命令添加到 PowerShell 配置文件。
若要安装包,代理需要允许与以下地址建立 HTTPS 连接:
https://www.powershellgallery.com
登录
若要开始使用 Azure PowerShell,请使用 Azure 凭据登录。
# Connect to Azure with a browser sign in token
Connect-AzAccount
注释
**
如果已禁用模块自动加载,请使用 Import-Module -Name Az 手动导入模块。
由于模块的结构方式,这可能需要几秒钟时间。
对于启动的每个新的 PowerShell 会话,需要重复这些步骤。 若要了解如何跨 PowerShell 会话保留 Azure 登录,请参阅 跨 PowerShell 会话保留用户凭据。
更新 Azure PowerShell 模块
若要更新任何 PowerShell 模块,应使用安装该模块的同一方法。 例如,如果最初使用 Install-Module过,则应使用 Update-Module 获取最新版本。 如果最初使用 MSI 包,则应下载并安装新的 MSI 包。
PowerShellGet cmdlet 无法更新从 MSI 包安装的模块。 MSI 包不会更新使用 PowerShellGet 安装的模块。 如果在使用 PowershellGet 更新时遇到任何问题,则应 重新安装,而不是 更新。 重新安装与安装方式相同,但需要添加 -Force 参数:
if (Get-Module -Name AzureRM -ListAvailable) {
Write-Warning -Message ('Az module not installed. Having both the AzureRM and ' +
'Az modules installed at the same time is not supported.')
} else {
Install-Module -Name Az -AllowClobber -Force
}
与基于 MSI 的安装不同,使用 PowerShellGet 安装或更新不会删除系统上可能存在的旧版本。 若要从系统中删除旧版 Azure PowerShell,请参阅 “卸载 Azure PowerShell”模块。 有关基于 MSI 的安装的详细信息,请参阅 使用 MSI 安装 Azure PowerShell。
使用多个版本的 Azure PowerShell
可以安装多个版本的 Azure PowerShell。 若要检查是否安装了多个版本的 Azure PowerShell,请使用以下命令:
Get-InstalledModule -Name Az -AllVersions | Select-Object -Property Name, Version
若要删除 Azure PowerShell 的版本,请参阅 卸载 Azure PowerShell 模块。
如果已安装模块的多个版本,则默认情况下,模块会自动加载并 Import-Module 加载最新版本。
可以使用Az参数安装或加载-RequiredVersion模块的特定版本:
# Install Az version 3.6.1
Install-Module -Name Az -RequiredVersion 3.6.1
# Load Az version 3.6.1
Import-Module -Name Az -RequiredVersion 3.6.1
提供反馈
如果在 Azure PowerShell 中发现 bug, 请将问题提交到 GitHub 上。 若要从命令行提供反馈,请使用 Send-Feedback cmdlet。
后续步骤
若要详细了解 Azure PowerShell 模块及其功能,请参阅 Azure PowerShell 入门。 如果熟悉 Azure PowerShell,需要从 AzureRM 迁移,请参阅 从 AzureRM 迁移到 Az。