安裝 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

警告

您__無法__同時為 PowerShell 5.1 for Windows 安裝 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 工作階段中以系統管理員身份執行;若使用 macOS 或 Linux 則使用 sudo 命令執行: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 Resource Manager 模組,並使這些模組的 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 封鎖連線Proxy blocks connection

如果 Install-Module 顯示錯誤指出無法連線到 PowerShell 資源庫,表示您可能在 Proxy 後方。If you get errors from Install-Module that indicate the PowerShell Gallery is unreachable, you may be behind a proxy. 不同的作業系統在設定全系統的 Proxy 時會有不同的需求,此處並未詳盡說明。Different operating systems will have different requirements for configuring a system-wide proxy, which are not covered in detail here. 請連絡系統管理員以取得您的 Proxy 設定,並詢問如何為您的 OS 進行其設定。Contact your system administrator for your proxy settings and how to configure them for your OS.

PowerShell 本身不一定會設定為自動使用此 Proxy。PowerShell itself may not be configured to use this proxy automatically. 使用 PowerShell 5.1 和更新版本時,請使用下列命令,設定要用於 PowerShell 工作階段的 Proxy: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

如果您的作業系統認證已正確設定,此時將會透過 Proxy 路由傳送 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.

若要安裝套件,您的 Proxy 必須允許下列位址的 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

由於 Az 模組的封裝方式,Update-module 命令將不會正確更新您的安裝。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 有錯誤,請在 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 PowerShellTo 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.