Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.25k stars 3.84k forks source link

`Get-AzContext -ListAvailable`: Failed with error `Object reference not set to an instance of an object` #24179

Closed MoChilia closed 7 months ago

MoChilia commented 8 months ago

Description

Get-AzContext -ListAvailable failed with error Object reference not set to an instance of an object. image Az: 9.3.0 Az.Accounts: 2.15.1 PSVersion: 7.4.1

Workflow file

The azure account should be valid and logged successfully since Get-AzContext works well.

Update: Get-AzContext -ListAvailable failed in both GitHub Action and local terminal in Az.Accounts 2.15.1.

Issue script & Debug output

DEBUG: Got version 0 of Az
DEBUG: Got version 0 of Az.Accounts
DEBUG: 02:15:56 - GetAzureRMContextCommand begin processing with ParameterSet 'ListAllContexts'.
DEBUG: 02:15:56 - [ConfigManager] Got nothing from [DisplayBreakingChangeWarning], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: 02:15:56 - [ConfigManager] Got nothing from [DisableErrorRecordsPersistence], Module = [], Cmdlet = []. Returning default value [False].
DEBUG: 02:15:56 - [ConfigManager] Got nothing from [EnableDataCollection], Module = [], Cmdlet = []. Returning default value [True].
Get-AzContext: /home/runner/work/_temp/e5e6bc94-f07f-4c52-91d5-2ff13926057b.ps1:7
Line |
   7 |  Get-AzContext -ListAvailable
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Object reference not set to an instance of an object.
Error: Error: The process '/usr/bin/pwsh' failed with exit code 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.1
PSEdition                      Core
GitCommitId                    7.4.1
OS                             Ubuntu 22.04.3 LTS
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Module versions

Name              : Az
Path              : /usr/share/az_9.3.0/Az/9.3.0/Az.psd1
Description       : Microsoft Azure PowerShell - Cmdlets to manage resources in
                     Azure. This module is compatible with PowerShell and Windo
                    ws PowerShell.
                    For more information about the Az module, please visit the 
                    following: https://docs.microsoft.com/powershell/azure/
Guid              : d48d710e-85cb-46a1-990f-22dae76f6b5f
Version           : 9.3.0
ModuleBase        : /usr/share/az_9.3.0/Az/9.3.0
ModuleType        : Script
PrivateData       : {[PSData, System.Collections.Hashtable]}
AccessMode        : ReadWrite
ExportedAliases   : {}
ExportedCmdlets   : {}
ExportedFunctions : {}
ExportedVariables : {}
NestedModules     : {}

Name              : Az.Accounts
Path              : /usr/share/az_9.3.0/Az.Accounts/2.15.1/Az.Accounts.psd1
Description       : Microsoft Azure PowerShell - Accounts credential management
                     cmdlets for Azure Resource Manager in Windows PowerShell a
                    nd PowerShell Core.

                    For more information on account credential management, plea
                    se visit the following: https://learn.microsoft.com/powersh
                    ell/azure/authenticate-azureps
Guid              : 17a2feff-488b-47f9-8729-e2cec094624c
Version           : 2.15.1
ModuleBase        : /usr/share/az_9.3.0/Az.Accounts/2.15.1
ModuleType        : Script
PrivateData       : {[PSData, System.Collections.Hashtable]}
AccessMode        : ReadWrite
ExportedAliases   : {[Add-AzAccount, Add-AzAccount], [Login-AzAccount, Login-Az
                    Account], [Remove-AzAccount, Remove-AzAccount], [Logout-AzA
                    ccount, Logout-AzAccount]…}
ExportedCmdlets   : {[Disable-AzDataCollection, Disable-AzDataCollection], [Dis
                    able-AzContextAutosave, Disable-AzContextAutosave], [Enable
                    -AzDataCollection, Enable-AzDataCollection], [Enable-AzCont
                    extAutosave, Enable-AzContextAutosave]…}
ExportedFunctions : {}
ExportedVariables : {}
NestedModules     : {}

Error output

DEBUG: 02:15:56 - [ConfigManager] Got nothing from [DisplayBreakingChangeWarning], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: 02:15:56 - [ConfigManager] Got nothing from [DisplayRegionIdentified], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: 02:15:56 - [ConfigManager] Got nothing from [CheckForUpgrade], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: AzureQoSEvent:  Module: Az.Accounts:2.15.1; CommandName: Get-AzContext; PSVersion: 7.4.1; IsSuccess: False; Duration: 00:00:00.7924944; Exception: Object reference not set to an instance of an object.;
DEBUG: 02:15:56 - GetAzureRMContextCommand end processing.
DEBUG: 02:15:56 - ResolveError begin processing with ParameterSet 'AnyErrorParameterSet'.
DEBUG: 02:15:56 - using account id '***'...
DEBUG: 02:15:56 - [ConfigManager] Got nothing from [DisplayBreakingChangeWarning], Module = [], Cmdlet = []. Returning default value [True].

InnerException   : False
Exception        : System.NullReferenceException: Object reference not set to a
                   n instance of an object.
                      at Microsoft.Azure.Commands.Profile.GetAzureRMContextComm
                   and.<>c.<ExecuteCmdlet>b__17_2(PSAzureContext context)
                      at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] 
                   elements, Int32 count)
                      at System.Linq.EnumerableSorter`1.ComputeMap(TElement[] e
                   lements, Int32 count)
                      at System.Linq.EnumerableSorter`1.Sort(TElement[] element
                   s, Int32 count)
                      at System.Linq.OrderedEnumerable`1.GetEnumerator()+MoveNe
                   xt()
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.Gener
                   alUtilities.ForEach[T](IEnumerable`1 sequence, Action`1 acti
                   on)
                      at Microsoft.Azure.Commands.Profile.GetAzureRMContextComm
                   and.WritePSAzureContext(IEnumerable`1 psAzureContext)
                      at Microsoft.Azure.Commands.Profile.GetAzureRMContextComm
                   and.ExecuteCmdlet()
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.Cmdle
                   tExtensions.<>c__3`1.<ExecuteSynchronouslyOrAsJob>b__3_0(T c
                   )
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.Cmdle
                   tExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet, Action`
                   1 executor)
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.Cmdle
                   tExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet)
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.Azure
                   PSCmdlet.ProcessRecord()
Message          : Object reference not set to an instance of an object.
StackTrace       :    at Microsoft.Azure.Commands.Profile.GetAzureRMContextComm
                   and.<>c.<ExecuteCmdlet>b__17_2(PSAzureContext context)
                      at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] 
                   elements, Int32 count)
                      at System.Linq.EnumerableSorter`1.ComputeMap(TElement[] e
                   lements, Int32 count)
                      at System.Linq.EnumerableSorter`1.Sort(TElement[] element
                   s, Int32 count)
                      at System.Linq.OrderedEnumerable`1.GetEnumerator()+MoveNe
                   xt()
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.Gener
                   alUtilities.ForEach[T](IEnumerable`1 sequence, Action`1 acti
                   on)
                      at Microsoft.Azure.Commands.Profile.GetAzureRMContextComm
                   and.WritePSAzureContext(IEnumerable`1 psAzureContext)
                      at Microsoft.Azure.Commands.Profile.GetAzureRMContextComm
                   and.ExecuteCmdlet()
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.Cmdle
                   tExtensions.<>c__3`1.<ExecuteSynchronouslyOrAsJob>b__3_0(T c
                   )
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.Cmdle
                   tExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet, Action`
                   1 executor)
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.Cmdle
                   tExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet)
                      at Microsoft.WindowsAzure.Commands.Utilities.Common.Azure
                   PSCmdlet.ProcessRecord()
HelpLink         : 
ErrorDetails     : 
ErrorCategory    : CloseError: (:) [Get-AzContext], NullReferenceException
InvocationInfo   : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, /home/runner/work/_temp/e5e6bc94-f07f-4c52
                   -91d5-2ff13926057b.ps1: line 7
                   at <ScriptBlock>, <No file>: line 1
MoChilia commented 7 months ago

Resolved in Az.Accounts 2.16.0. See https://github.com/Azure/azclitools-actions-test/actions/runs/8340363505/job/22824150533

djkrivak commented 6 months ago

I have the same problem on Windows. Using mainly PS core 7.4.2, but I get the same result in 5.1.

PSVersion table

Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Az.Accounts version

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.17.0                Az.Accounts                         {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault…}

Get-AzContext -ListAvailable error

Get-AzContext -ListAvailable -Debug
DEBUG: 10:04:28 - GetAzureRMContextCommand begin processing with ParameterSet 'ListAllContexts'.
DEBUG: 10:04:28 - [ConfigManager] Got nothing from [DisplayBreakingChangeWarning], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: 10:04:28 - [ConfigManager] Got nothing from [DisableErrorRecordsPersistence], Module = [], Cmdlet = []. Returning default value [False].
DEBUG: 10:04:28 - [ConfigManager] Got nothing from [EnableDataCollection], Module = [], Cmdlet = []. Returning default value [True].
Get-AzContext: Object reference not set to an instance of an object.
DEBUG: 10:04:28 - [ConfigManager] Got nothing from [DisplayBreakingChangeWarning], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: 10:04:28 - [ConfigManager] Got nothing from [DisplayRegionIdentified], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: 10:04:28 - [ConfigManager] Got nothing from [CheckForUpgrade], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: AzureQoSEvent:  Module: Az.Accounts:2.17.0; CommandName: Get-AzContext; PSVersion: 7.4.2; IsSuccess: False; Duration: 00:00:00.0342198; Exception: Object reference not set to an instance of an object.;
DEBUG: 10:04:29 - GetAzureRMContextCommand end processing.