@@ -173,6 +173,9 @@ public override void ExecuteCmdlet()
173
173
{
174
174
ExecutionBlock ( ( ) =>
175
175
{
176
+ //safe gauard for login status, check if DefaultContext not existed, PSInvalidOperationException will be thrown
177
+ var CheckDefaultContext = DefaultContext ;
178
+
176
179
if ( this . ParameterSetName == SimpleParameterSet )
177
180
{
178
181
CreateSimpleServicePrincipal ( ) ;
@@ -248,21 +251,7 @@ public override void ExecuteCmdlet()
248
251
249
252
private void CreateSimpleServicePrincipal ( )
250
253
{
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 ;
266
255
if ( ! this . IsParameterBound ( c => c . StartDate ) )
267
256
{
268
257
DateTime currentTime = DateTime . UtcNow ;
@@ -327,11 +316,29 @@ private void CreateSimpleServicePrincipal()
327
316
}
328
317
} ;
329
318
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 " +
334
339
"having AppId '{0}' with '{1}' role over scope '{2}'." , createParameters . ApplicationId , this . Role , this . Scope ) ;
340
+ }
341
+
335
342
if ( ShouldProcess ( target : createParameters . ApplicationId . ToString ( ) , action : shouldProcessMessage ) )
336
343
{
337
344
PSADServicePrincipalWrapper servicePrincipal = new PSADServicePrincipalWrapper ( ActiveDirectoryClient . CreateServicePrincipal ( createParameters ) ) ;
@@ -383,7 +390,12 @@ private void CreateSimpleServicePrincipal()
383
390
384
391
private bool SkipRoleAssignment ( )
385
392
{
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 ;
387
399
}
388
400
}
389
401
}
0 commit comments