@@ -619,9 +619,11 @@ try {
619
619
Write-Warning " The specified TestApplicationId '$TestApplicationId ' will be ignored when UserAuth is set."
620
620
}
621
621
622
- $TestApplicationOid = (Get-AzADUser - UserPrincipalName (Get-AzContext ).Account).Id
622
+ $userAccount = (Get-AzADUser - UserPrincipalName (Get-AzContext ).Account)
623
+ $TestApplicationOid = $userAccount.Id
623
624
$TestApplicationId = $testApplicationOid
624
- Log " User-based app id '$TestApplicationId ' will be used."
625
+ $userAccountName = $userAccount.UserPrincipalName
626
+ Log " User authentication with user '$userAccountName ' ('$TestApplicationId ') will be used."
625
627
}
626
628
# If no test application ID was specified during an interactive session, create a new service principal.
627
629
elseif (! $CI -and ! $TestApplicationId ) {
@@ -686,11 +688,11 @@ try {
686
688
$PSBoundParameters [' TestApplicationOid' ] = $TestApplicationOid
687
689
$PSBoundParameters [' TestApplicationSecret' ] = $TestApplicationSecret
688
690
689
- # If the role hasn't been explicitly assigned to the resource group and a cached service principal is in use,
691
+ # If the role hasn't been explicitly assigned to the resource group and a cached service principal or user authentication is in use,
690
692
# query to see if the grant is needed.
691
- if (! $resourceGroupRoleAssigned -and $AzureTestPrincipal ) {
693
+ if (! $resourceGroupRoleAssigned -and $TestApplicationOid ) {
692
694
$roleAssignment = Get-AzRoleAssignment `
693
- - ObjectId $AzureTestPrincipal .Id `
695
+ - ObjectId $TestApplicationOid `
694
696
- RoleDefinitionName ' Owner' `
695
697
- ResourceGroupName " $ResourceGroupName " `
696
698
- ErrorAction SilentlyContinue
@@ -702,19 +704,20 @@ try {
702
704
# considered a critical failure, as the test application may have subscription-level permissions and not require
703
705
# the explicit grant.
704
706
if (! $resourceGroupRoleAssigned ) {
705
- Log " Attempting to assigning the 'Owner' role for '$ResourceGroupName ' to the Test Application '$TestApplicationId '"
706
- $principalOwnerAssignment = New-AzRoleAssignment `
707
- - RoleDefinitionName " Owner" `
708
- - ApplicationId " $TestApplicationId " `
709
- - ResourceGroupName " $ResourceGroupName " `
710
- - ErrorAction SilentlyContinue
711
-
712
- if ($principalOwnerAssignment.RoleDefinitionName -eq ' Owner' ) {
713
- Write-Verbose " Successfully assigned ownership of '$ResourceGroupName ' to the Test Application '$TestApplicationId '"
707
+ $idSlug = if ($userAuth ) { " User '$userAccountName ' ('$TestApplicationId ')" } else { " Test Application '$TestApplicationId '" };
708
+ Log " Attempting to assign the 'Owner' role for '$ResourceGroupName ' to the $idSlug "
709
+ $ownerAssignment = New-AzRoleAssignment `
710
+ - RoleDefinitionName " Owner" `
711
+ - ObjectId " $TestApplicationOId " `
712
+ - ResourceGroupName " $ResourceGroupName " `
713
+ - ErrorAction SilentlyContinue
714
+
715
+ if ($ownerAssignment.RoleDefinitionName -eq ' Owner' ) {
716
+ Write-Verbose " Successfully assigned ownership of '$ResourceGroupName ' to the $idSlug "
714
717
} else {
715
718
Write-Warning (" The 'Owner' role for '$ResourceGroupName ' could not be assigned. " +
716
719
" You may need to manually grant 'Owner' for the resource group to the " +
717
- " Test Application ' $TestApplicationId ' if it does not have subscription-level permissions." )
720
+ " $idSlug if it does not have subscription-level permissions." )
718
721
}
719
722
}
720
723
0 commit comments