針對 Az PowerShell 模組進行疑難解答

啟用偵錯記錄

針對 Azure Az PowerShell 模組的問題進行疑難解答時,您應該採取的第一個步驟是啟用偵錯記錄。

若要根據每個命令啟用偵錯記錄,請指定 Debug 參數。

Get-AzResource -Name 'DoesNotExist' -Debug

若要開啟整個 PowerShell 工作階段的 偵錯記錄,請將 DebugPreference 變數的值設定為 Continue

$DebugPreference = 'Continue'

安裝

本節包含安裝 Azure Az PowerShell 模組時常見問題的解決方案清單。

Az 和 AzureRM 共存

警告

我們不支援在 Windows PowerShell 5.1 中同時安裝 AzureRM 和 Az PowerShell 模組。

在需要在同一個 Windows 系統上安裝 AzureRM 和 Az PowerShell 模組的案例中:

  • AzureRM 只能安裝在 Windows PowerShell 5.1 的目前用戶範圍中。
  • 在 PowerShell 7.2 或更高版本中安裝 Az PowerShell 模組。

警告

自 2024 年 2 月 29 日起,AzureRM PowerShell 模組已正式淘汰。 建議使用者從 AzureRM 遷移至 Az PowerShell 模組,以確保持續支援和更新。

雖然 AzureRM 模組可能仍可運作,但不再維護或支援它,但會根據用戶的判斷權和風險放置任何繼續使用。 如需轉換至 Az 模組的指引,請參閱我們的 移轉資源

Visual Studio

舊版 Visual Studio 可能會安裝 Azure PowerShell 作為 Azure 開發工作負載的一部分,以安裝 AzureRM 模組。 您可以使用 Visual Studio 安裝程式或使用應用程式與功能中的「卸載」來移除 Azure PowerShell。 如果您已安裝 PowerShell 7.x,您可能需要 手動安裝 Az PowerShell 模組。

Proxy 封鎖連線

如果您收到無法連線到 PowerShell 資源庫 的錯誤Install-Module,您可能會位於 Proxy 後方。 不同的作業系統和網路環境在設定全系統的 Proxy 時會有不同的需求。 請連絡系統管理員以取得您的 Proxy 設定,並詢問如何為您的作業系統進行其設定。

PowerShell 本身可能未設定為自動使用此 Proxy。 使用 PowerShell 5.1 和更新版本,並使用下列命令,將 PowerShell 工作階段設定為使用 Proxy:

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

如果您的操作系統認證已正確設定,此設定會透過 Proxy 路由傳送 PowerShell 要求。 若要讓此設定在會話之間保存,請將命令新增至PowerShell 配置檔

若要安裝套件,您的 Proxy 必須允許 HTTPS 連線 www.powershellgallery.com

未將物件參考設定為物件的執行個體

「物件參考未設定為對象的實例」訊息表示您參考的物件為 Null 或不存在的 Azure 資源,或您沒有存取權限的物件。

$resourceId =  '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/<resource-group-name>/providers/Microsoft.Web/sites/<webapp-name>/privateEndpointConnections/<endpoint-name>'
Get-AzPrivateEndpointConnection -ResourceId $resourceId
Get-AzPrivateEndpointConnection: Object reference not set to an instance of an object.

您可以使用 Get-AzResource Cmdlet 來確認指定的 Azure 資源存在。

Get-AzResource -ResourceId $resourceId

AzAD Cmdlet 的許可權問題

Az PowerShell 模組會使用 Microsoft Graph API。 管理員 使用 Az PowerShell 模組在 Azure 中註冊或管理資源,需要與從 Azure 入口網站 或任何其他 Azure 命令行工具執行相同工作的許可權相同。 如需許可權的特定問題,請參閱 Microsoft Graph 許可權參考

Microsoft Graph 查詢參數

Az.Resources 下的 AzAd Cmdlet 現在支援 查詢參數搜尋查詢參數。 如需語法的詳細資訊,請參閱先前參考的連結。

Get-AzAdGroupMember 不會傳回服務主體

由於目前圖形 API 的限制,Az 7.x 中的 Get-AzAdGroupMember 不會傳回服務主體。 因應措施 是 Invoke-AzRestMethod 可以搭配 Microsoft Graph API 的 Beta 版本使用。

下列範例需要 Az PowerShell 模組。 將第一行中的 取代 myGroupName 為您群組的名稱。

$Group = Get-AzADGroup -DisplayName myGroupName
((Invoke-AzRestMethod -Uri "https://graph.microsoft.com/beta/groups/$($Group.id)/members").Content |
  ConvertFrom-Json).value |
  Select-Object -Property DisplayName, Id, @{label='OdataType';expression={$_.'@odata.type'}}

找到但無法載入的命令

當您嘗試執行任何 Az PowerShell 命令時,PowerShell 會傳回下列訊息。

Connect-AzAccount: The 'Connect-AzAccount' command was found in the module 'Az.Accounts', but the module could not be loaded. For more information, run 'Import-Module Az.Accounts'.

當您在同一個 Windows 系統上同時安裝 Az 和 AzureRM PowerShell 模組,且它們存在於相同版本的 PowerShell $env:PSModulePath時,就會發生此訊息。

重要

當 AzureRM 安裝在 AllUsers Windows PowerShell 的範圍內時,它會安裝在 PowerShell 7 的 $env:PSModulePath 一部分位置。 由於 AzureRM 和 Az PowerShell 模組之間會發生衝突,因此無法支援。

Az 和 AzureRM 可以共存於相同的 Windows 系統上,但只有在 AzureRM 安裝在 CurrentUser PowerShell 7 的 Windows PowerShell 和 Az 範圍內時才存在。 如需詳細資訊,請參閱 安裝 Az PowerShell 模組

警告

自 2024 年 2 月 29 日起,AzureRM PowerShell 模組已正式淘汰。 建議使用者從 AzureRM 遷移至 Az PowerShell 模組,以確保持續支援和更新。

雖然 AzureRM 模組可能仍可運作,但不再維護或支援它,但會根據用戶的判斷權和風險放置任何繼續使用。 如需轉換至 Az 模組的指引,請參閱我們的 移轉資源

在 MacOS 上,KeyChain 授權失敗時傳回錯誤

在 MacOS 上執行 Azure PowerShell 時,您可能會在嘗試從 PowerShell 會話登入 Azure 帳戶時遇到錯誤訊息。

DeviceCodeCredential authentication failed: Persistence check failed. Reason: KeyChain authorization/authentication failed. .Error code: -25293. OS error code -25293.

此問題的因應措施為執行下列命令,來停用在工作階段間儲存認證。 不過,進行這項變更之後,您必須在每次啟動新的 PowerShell 工作階段時執行 Connect-AzAccount

Disable-AzContextAutosave

此網站的連線不安全

當您的默認瀏覽器是 Microsoft Edge 時,嘗試以互動方式 Connect-AzAccount登入 Azure 時,可能會遇到下列錯誤:「此網站的連線不安全」。若要解決此問題,請造訪 Microsoft Edge 中的 edge://net-internals/#hsts 。 在 [刪除網域安全策略] 底下新增 localhost ,然後按兩下 [刪除]。

服務主體IdentifierUri已驗證網域錯誤

錯誤:執行 或 New-AzADApplicationNew-AzADServicePrincipal,identifierUris 屬性的值必須使用組織的已驗證網域或其子域

由於 Microsoft Entra 重大變更要求 單一租使用者應用程式中的 AppId URI 需要使用預設配置或已驗證網域 ,您必須將 Az.Resources 模組升級至 4.1.0 版或更新版本,才能繼續使用 New-AzADServicePrincipalNew-AzADApplication Cmdlet。

您也可以升級至 Az PowerShell 模組 6.0 版或更新版本。

時間軸

該要求於2021年10月15日生效。

受影響的版本

下列版本的 Azure PowerShell 會受到 AzureAD 重大變更的影響:

  • Az.Resources PowerShell 模組版本 3.5.1-preview 或更少。
  • Az PowerShell 模組 5.9.0 版或更少。

如果您在升級后仍然遇到問題,請隨意開啟 問題

因應措施

如果您無法升級至先前所述的 PowerShell 模組,您可以在建立服務主體時遵循這些步驟:

其他問題

如果您遇到本文未列出的 Azure PowerShell 產品問題,或需要進一步協助, 請在 GitHub 上提出問題。