Skip to content

Commit 70ae49c

Browse files
committed
Return null if DefaultProfile or context does not exist, move error writing to ExecuteCmdlet
1 parent 1994740 commit 70ae49c

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/ResourceManager/Profile/Commands.Profile/Context/GetAzureRMContext.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ protected override AzureContext DefaultContext
3636
{
3737
if (DefaultProfile == null || DefaultProfile.Context == null)
3838
{
39-
WriteError(new ErrorRecord(
40-
new PSInvalidOperationException("Run Login-AzureRmAccount to login."),
41-
string.Empty,
42-
ErrorCategory.AuthenticationError,
43-
null));
39+
return null;
4440
}
4541

4642
return DefaultProfile.Context;
@@ -49,7 +45,16 @@ protected override AzureContext DefaultContext
4945

5046
public override void ExecuteCmdlet()
5147
{
52-
WriteObject((PSAzureContext)AzureRmProfileProvider.Instance.Profile.Context);
48+
var context = (PSAzureContext)AzureRmProfileProvider.Instance.Profile.Context;
49+
if (context == null)
50+
{
51+
WriteError(new ErrorRecord(
52+
new PSInvalidOperationException("Run Login-AzureRmAccount to login."),
53+
string.Empty,
54+
ErrorCategory.AuthenticationError,
55+
null));
56+
}
57+
WriteObject(context);
5358
}
5459
}
5560
}

0 commit comments

Comments
 (0)