@@ -580,14 +580,18 @@ try {
580580 $PSBoundParameters [' TestApplicationOid' ] = $TestApplicationOid
581581 $PSBoundParameters [' TestApplicationSecret' ] = $TestApplicationSecret
582582
583- # Grant the test service principal ownership over the resource group. This may fail if the provisioner is a
584- # service principal without permissions to grant RBAC roles to other service principals. That should not be
585- # considered a critical failure, as the test application may have subscription-level permissions and not require
586- # the explicit grant.
587- #
588- # Ignore this check if $AzureTestPrincipal is specified as role assignment will already have been attempted on a
589- # previous run, and these error messages can be misleading for local runs.
590- if (! $resourceGroupRoleAssigned -and ! $AzureTestPrincipal ) {
583+ # If the role hasn't been explicitly assigned to the resource group and a cached service principal is in use,
584+ # query to see if the grant is needed.
585+ if (! $resourceGroupRoleAssigned -and $AzureTestPrincipal ) {
586+ $roleAssignment = Get-AzRoleAssignment - ObjectId $AzureTestPrincipal.Id - RoleDefinitionName ' Owner' - ResourceGroupName " $ResourceGroupName " - ErrorAction SilentlyContinue
587+ $resourceGroupRoleAssigned = ($roleAssignment.RoleDefinitionName -eq ' Owner' )
588+ }
589+
590+ # If needed, grant the test service principal ownership over the resource group. This may fail if the provisioner
591+ # is a service principal without permissions to grant RBAC roles to other service principals. That should not be
592+ # considered a critical failure, as the test application may have subscription-level permissions and not require
593+ # the explicit grant.
594+ if (! $resourceGroupRoleAssigned ) {
591595 Log " Attempting to assigning the 'Owner' role for '$ResourceGroupName ' to the Test Application '$TestApplicationId '"
592596 $principalOwnerAssignment = New-AzRoleAssignment - RoleDefinitionName " Owner" - ApplicationId " $TestApplicationId " - ResourceGroupName " $ResourceGroupName " - ErrorAction SilentlyContinue
593597
0 commit comments