Azure / Azure/azure-powershell

A corrupted AzureRMContextContext.json file breaks Connect-AzAccount -Environment '<env>'

Open
#22,124 1 comment 0 reactions 0 assignees View on GitHub
customer-reported feature-request
Dominant language
C#
Stars
4.8k
Forks
4.3k
Avg merge
2d 17h
Merged PRs (30d)
51

Description

### Description

We encountered this on one of our deployment VMs. My guess is that one of the VMs crashed mid-write or something and the file became corrupted.

The deserialization here swallows the jsonexception and returns false (and initializes an empty class):
https://github.com/Azure/azure-powershell/blob/d5e111d35aec112bdeeb71c0bcb42fd5857a59d1/src/Accounts/Authentication.ResourceManager/AzureRmProfile.cs#LL178C1-L186C14

The caller here then sees the result value of false and doesn't call Initialize(profile)
https://github.com/Azure/azure-powershell/blob/main/src/Accounts/Authentication.ResourceManager/AzureRmProfile.cs#L149-L154

This call is what is required to add the default environments to a new profile, and so the returned profile has no environments.

This means that any call to Connect-AzAccount -Environment '' returns
Unable to find environment with name ''

The only recovery option right now is to delete the file. What I would recommend is at least seeing that if we new() in SafeDeserialize() that we also call Inititialize(profile) so that the profile becomes usable. As this issue stands, once you get in this state it's very obscure and Azure Powershell becomes completely unusable until the file is deleted.

### Issue script & Debug output

```PowerShell
C:\Users\macromullet> Get-AzContext
PS C:\Users\macromullet>
PS C:\Users\macromullet> $DebugPreference="Continue"
PS C:\Users\macromullet> Get-AzContext
DEBUG: 9:33:34 AM - GetAzureRMContextCommand end processing.
PS C:\Users\macromullet> Connect-AzAccount -Environment AzureCloud
DEBUG: Got version 0 of Az
DEBUG: Got version 0 of Az.Accounts
DEBUG: 9:33:42 AM - ConnectAzureRmAccountCommand begin processing with ParameterSet 'UserWithSubscriptionId'.
DEBUG: 9:33:42 AM - [ConfigManager] Got nothing from [DisplayBreakingChangeWarning], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: 9:33:42 AM - Autosave setting from startup session: 'CurrentUser'
DEBUG: 9:33:42 AM - No autosave setting detected in environment variable 'AzContextAutoSave'.
DEBUG: 9:33:42 AM - Using Autosave scope 'CurrentUser'
Connect-AzAccount: Unable to find environment with name 'AzureCloud'
PS C:\Users\macromullet> Connect-AzAccount -Environment AzureUSGovernment
DEBUG: 9:33:48 AM - ConnectAzureRmAccountCommand begin processing with ParameterSet 'UserWithSubscriptionId'.
DEBUG: 9:33:48 AM - [ConfigManager] Got nothing from [DisplayBreakingChangeWarning], Module = [], Cmdlet = []. Returning default value [True].
DEBUG: 9:33:48 AM - Autosave setting from startup session: 'CurrentUser'
DEBUG: 9:33:48 AM - No autosave setting detected in environment variable 'AzContextAutoSave'.
DEBUG: 9:33:48 AM - Using Autosave scope 'CurrentUser'
Connect-AzAccount: Unable to find environment with name 'AzureUSGovernment'
```

### Environment data

```PowerShell
Name Value
---- -----
PSVersion 7.3.4
PSEdition Core
GitCommitId 7.3.4
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
```

### Module versions

```PowerShell
ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 2.12.3 Az.Accounts {Add-AzEnvironment, Clear-AzConfig, Clear-AzConte…
Script 1.0.1 Az.Tools.Predictor {Disable-AzPredictor, Enable-AzPredictor, Open-Az…
```

### Error output

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.