Skip to content

Commit 0335d01

Browse files
authored
Merge pull request Azure#10492 from VeryEarly/nullreferenceexception-in-NewAZADServicePrincipal#9478
Fix bug in New-AZADServicePrincipal#9478
2 parents 14c739d + d17ba52 commit 0335d01

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

src/Resources/Resources/ActiveDirectory/Cmdlets/NewAzureADServicePrincipalCommand.cs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ public override void ExecuteCmdlet()
173173
{
174174
ExecutionBlock(() =>
175175
{
176+
//safe gauard for login status, check if DefaultContext not existed, PSInvalidOperationException will be thrown
177+
var CheckDefaultContext = DefaultContext;
178+
176179
if (this.ParameterSetName == SimpleParameterSet)
177180
{
178181
CreateSimpleServicePrincipal();
@@ -248,21 +251,7 @@ public override void ExecuteCmdlet()
248251

249252
private void CreateSimpleServicePrincipal()
250253
{
251-
var subscriptionId = DefaultProfile.DefaultContext.Subscription.Id;
252-
if (!this.IsParameterBound(c => c.Scope))
253-
{
254-
Scope = string.Format("/subscriptions/{0}", subscriptionId);
255-
WriteVerbose(string.Format("No scope provided - using the default scope '{0}'", Scope));
256-
}
257-
258-
AuthorizationClient.ValidateScope(Scope, true);
259-
260-
if (!this.IsParameterBound(c => c.Role))
261-
{
262-
Role = "Contributor";
263-
WriteVerbose(string.Format("No role provided - using the default role '{0}'", Role));
264-
}
265-
254+
var subscriptionId = DefaultContext.Subscription?.Id;
266255
if (!this.IsParameterBound(c => c.StartDate))
267256
{
268257
DateTime currentTime = DateTime.UtcNow;
@@ -327,11 +316,29 @@ private void CreateSimpleServicePrincipal()
327316
}
328317
};
329318

330-
var shouldProcessMessage = SkipRoleAssignment() ?
331-
string.Format("Adding a new service principal to be associated with an application " +
332-
"having AppId '{0}' with no permissions.", createParameters.ApplicationId) :
333-
string.Format("Adding a new service principal to be associated with an application " +
319+
var shouldProcessMessage = string.Format("Adding a new service principal to be associated with an application " +
320+
"having AppId '{0}' with no permissions.", createParameters.ApplicationId);
321+
322+
if (!SkipRoleAssignment())
323+
{
324+
if (!this.IsParameterBound(c => c.Scope))
325+
{
326+
Scope = string.Format("/subscriptions/{0}", subscriptionId);
327+
WriteVerbose(string.Format("No scope provided - using the default scope '{0}'", Scope));
328+
}
329+
330+
AuthorizationClient.ValidateScope(Scope, true);
331+
332+
if (!this.IsParameterBound(c => c.Role))
333+
{
334+
Role = "Contributor";
335+
WriteVerbose(string.Format("No role provided - using the default role '{0}'", Role));
336+
}
337+
338+
shouldProcessMessage = string.Format("Adding a new service principal to be associated with an application " +
334339
"having AppId '{0}' with '{1}' role over scope '{2}'.", createParameters.ApplicationId, this.Role, this.Scope);
340+
}
341+
335342
if (ShouldProcess(target: createParameters.ApplicationId.ToString(), action: shouldProcessMessage))
336343
{
337344
PSADServicePrincipalWrapper servicePrincipal = new PSADServicePrincipalWrapper(ActiveDirectoryClient.CreateServicePrincipal(createParameters));
@@ -383,7 +390,12 @@ private void CreateSimpleServicePrincipal()
383390

384391
private bool SkipRoleAssignment()
385392
{
386-
return this.IsParameterBound(c => c.SkipAssignment) || (!this.IsParameterBound(c => c.Role) && !this.IsParameterBound(c => c.Scope));
393+
return this.IsParameterBound(c => c.SkipAssignment) || (!this.IsParameterBound(c => c.Role) && !this.IsParameterBound(c => c.Scope) && !HasSubscription());
394+
}
395+
396+
private bool HasSubscription()
397+
{
398+
return DefaultContext.Subscription?.Id != null;
387399
}
388400
}
389401
}

src/Resources/Resources/ChangeLog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
## Upcoming Release
2121
- Updated policy cmdlets to use new api version 2019-06-01 that has new EnforcementMode property in policy assignment.
2222
- Updated create policy definition help example
23-
- Fix bug Remove-AZADServicePrincipal -ServicePrincipalName, throw null reference when service principal name not found.
23+
* Fix bug Remove-AZADServicePrincipal -ServicePrincipalName, throw null reference when service principal name not found.
24+
* Fix bug New-AZADServicePrincipal,throw null reference when tenant doesn't have any subscription.
2425

2526
## Version 1.7.1
2627
* Update dependency assembly Microsoft.Extensions.Caching.Memory from 1.1.1 to 2.2

0 commit comments

Comments
 (0)