Get-AzureRmContext should not throw when no context exists and "-ErrorAction SilentlyContinue" is used
Context
- Windows Server 2012 R2
- PowerShell v5.0.10514.6
- AzureRM v1.4
Steps to reproduce In a new PowerShell session, execute:
$context = Get-AzureRmContext -ErrorAction SilentlyContinue
Expected $context should be $null and no errors should be thrown about the need to run Login-AzureRmAccount prior to my call.
Intended scenario(s) Enable us to determine if a context is present or not without encountering an error. Could be used to support a clean persisted/restore profile mechanism.
Actual behavior
PS C:\Windows\system32> Get-AzureRmContext -ErrorAction SilentlyContinue
Get-AzureRmContext : Run Login-AzureRmAccount to login.
At line:1 char:1
+ Get-AzureRmContext -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-AzureRmContext], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.Profile.GetAzureRMContextCommand
+1 Seeing similar issue here.
@slapointe To run Get-AzureRmContext requires user login(context must be present). If it is not present PowerShell has to terminate execution and throw exception. And this is a terminating error by design which means it will be thrown anyway. In your scenario, my suggestion is to add a try-catch block for the cmdlet. Then you are able to determine if context is present by the checking this exception.
A good alternative would be to introduce a Test-AzureRmContext, what do you think?
@slapointe @haocs In general, not having a context is a terminating error - meaning that any cmdlet pipeline should be stopped immediately regardless of error settings. In this particular case, since the work of the cmdlet is just getting the context, I agree that ErrorActionPreference should apply in this narrow case. @haocs This probably sounds different from our previous communication, let's discuss.
@markcowl Note that $ErrorActionPreference does work in this case, it's the -ErrorAction parameter which does not work. This is clearly a bug, not a feature, there is no reason why this particular cmdlet should behave differently from other Get-* Azure PS cmdlets.
@slapointe Here is the workaround I'm using:
$currentActionPreference = $ErrorActionPreference
$ErrorActionPreference = 'silentlycontinue'
$context = Get-AzureRmContext
$ErrorActionPreference = $currentActionPreference
The fix here is to have the cmdlet override the DefaultContext property from AzureRmCmdlet so that it writes the message to the Error stream rather than throwing a terminating error.
Fixed
Fix checked in, will be out with next release
So this change has broken my script as I catch and handle the error to determine if there is a user logged on in the powershell session. I'll have to check for $null instead of an error.
I agree with @slapointe. A better option would be Test-AzureRmContext.
No biggy to fix, just annoying that the change isn't documented in the release notes (unless I'm looking in the wrong place (powershellgallery.com).
@arcotek-ltd Hey Adrian, sorry about not mentioning this anywhere for the release (usually these changes are announced on powershellgallery.com or in the change log). This was not considered a "breaking change" when we decided to take the pull request for the release, but now looking at how the change affects your script, it can definitely be treated as such. I will open a pull request today to add this change to the change log, as well as the migration guide that we send out with all breaking change releases.
Please let me know if you have any other questions!
The description of the change implies that Get-AzureRMContext without having logged in should return $null, but the behavior I am seeing is the return of a PSAzureContext object whose properties are all null/empty.