diff --git a/src/Storage/Commands.Storage/Common/Cmdlet/NewAzureStorageContext.cs b/src/Storage/Commands.Storage/Common/Cmdlet/NewAzureStorageContext.cs index ace513e68ac3..d951b6ef8797 100644 --- a/src/Storage/Commands.Storage/Common/Cmdlet/NewAzureStorageContext.cs +++ b/src/Storage/Commands.Storage/Common/Cmdlet/NewAzureStorageContext.cs @@ -341,15 +341,22 @@ internal CloudStorageAccount GetStorageAccountWithAzureEnvironment(StorageCreden var profile = SMProfile??RMProfile; if (null != profile) { - if (DefaultContext != null && DefaultContext.Environment != null && string.IsNullOrEmpty(azureEnvironmentName)) + try { - azureEnvironment = DefaultContext.Environment; - - if (null == azureEnvironment) + if (DefaultContext != null && DefaultContext.Environment != null && string.IsNullOrEmpty(azureEnvironmentName)) { - azureEnvironmentName = EnvironmentName.AzureCloud; + azureEnvironment = DefaultContext.Environment; + + if (null == azureEnvironment) + { + azureEnvironmentName = EnvironmentName.AzureCloud; + } } } + catch (InvalidOperationException) + { + //When can't get DefaultContext will use other ways to get AzureEnvironment + } if (null == azureEnvironment) {