安裝 Azure PowerShell

本文說明如何使用 PowerShellGet 安裝 Azure PowerShell 模組。 這些指示適用於 Windows、Linux 和 macOS 平臺。

Azure Cloud Shell 也提供 Azure PowerShell。

需求

Azure PowerShell 可在 Windows 上使用 PowerShell 5.1 或更高版本,或在所有平臺上使用 PowerShell Core 6.x 和更新版本。 您應該為您的作業系統安裝最新版本的 PowerShell。 在 PowerShell 6.2.4 和更新版本上執行時,Azure PowerShell 沒有其他需求。

若要檢查 PowerShell 版本,請執行命令:

$PSVersionTable.PSVersion

若要在 Windows 上的 PowerShell 5.1 中使用 Azure PowerShell:

  1. 更新為 Windows PowerShell 5.1。 如果您使用的是 Windows 10 版本 1607 或更新版本,則已安裝 PowerShell 5.1。
  2. 安裝 .NET Framework 4.7.2 或更新版本。
  3. 請確定您有最新版的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 中,使用 以系統管理員身分執行來啟動 PowerShell 會話;在 macOS 或 Linux 上,使用 sudo 命令來啟動:

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 上提出問題。

Proxy 封鎖連線

如果您收到錯誤 Install-Module,指出 PowerShell 資源庫無法連線,您可能會受到代理伺服器限制。 不同的作系統和網路環境對於設定全系統 Proxy 有不同的需求。 請連絡您的系統管理員以取得 Proxy 設定,以及如何為您的環境設定它們。

PowerShell 本身可能未設定為自動使用此 Proxy。 使用 PowerShell 5.1 和更新版本時,請使用下列命令,將 PowerShell 會話設定為使用 Proxy:

$webClient = New-Object System.Net.WebClient
$webClient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

如果您的作業系統憑證已正確設定,此設定會透過代理路由傳送 PowerShell 要求。 若要讓工作階段之間保留此設定,請將命令新增至 PowerShell 設定檔。

若要安裝套件,您的 Proxy 必須允許 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 中發現錯誤, 請在 GitHub 上提出問題。 若要從命令行提供意見反應,請使用 Send-Feedback Cmdlet。

後續步驟

若要深入瞭解 Azure PowerShell 模組及其功能,請參閱 開始使用 Azure PowerShell。 如果您熟悉 Azure PowerShell,且需要從 AzureRM 移轉,請參閱 從 AzureRM 遷移至 Az。