Skip to content

Commit 22737ba

Browse files
azure-sdkbenbp
andauthored
Sync eng/common directory with azure-sdk-tools for PR 2513 (Azure#22392)
* Update test resources SP password creation to support Az >= 7.1.0 * Check Az.Resources module version Co-authored-by: Ben Broderick Phillips <[email protected]>
1 parent a93084d commit 22737ba

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

eng/common/TestResources/New-TestResources.ps1

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,24 @@ function NewServicePrincipalWrapper([string]$subscription, [string]$resourceGrou
143143
$spPassword = $servicePrincipal.Secret
144144
$appId = $servicePrincipal.ApplicationId
145145
} else {
146-
Write-Verbose "Creating password for service principal via MS Graph API"
147-
# Microsoft graph objects (Az version >= 7.0.0) do not provision a secret # on creation so it must be added separately.
148-
# Submitting a password credential object without specifying a password will result in one being generated on the server side.
149-
$password = New-Object -TypeName "Microsoft.Azure.PowerShell.Cmdlets.Resources.MSGraph.Models.ApiV10.MicrosoftGraphPasswordCredential"
150-
$password.DisplayName = "Password for $displayName"
151-
$credential = Retry { New-AzADSpCredential -PasswordCredentials $password -ServicePrincipalObject $servicePrincipal }
152-
$spPassword = ConvertTo-SecureString $credential.SecretText -AsPlainText -Force
153-
$appId = $servicePrincipal.AppId
146+
if ((Get-Module Az.Resources).Version -eq "5.1.0") {
147+
Write-Verbose "Creating password and credential for service principal via MS Graph API"
148+
Write-Warning "Please update Az.Resources to >= 5.2.0 by running 'Update-Module Az'"
149+
# Microsoft graph objects (Az.Resources version == 5.1.0) do not provision a secret on creation so it must be added separately.
150+
# Submitting a password credential object without specifying a password will result in one being generated on the server side.
151+
$password = New-Object -TypeName "Microsoft.Azure.PowerShell.Cmdlets.Resources.MSGraph.Models.ApiV10.MicrosoftGraphPasswordCredential"
152+
$password.DisplayName = "Password for $displayName"
153+
$credential = Retry { New-AzADSpCredential -PasswordCredentials $password -ServicePrincipalObject $servicePrincipal }
154+
$spPassword = ConvertTo-SecureString $credential.SecretText -AsPlainText -Force
155+
$appId = $servicePrincipal.AppId
156+
} else {
157+
Write-Verbose "Creating service principal credential via MS Graph API"
158+
# In 7.1.0 the password credential issue was fixed (see https://github.com/Azure/azure-powershell/pull/16690) but the
159+
# parameter set was changed making the above call fail due to a missing ServicePrincipalId parameter.
160+
$credential = Retry { $servicePrincipal | New-AzADSpCredential }
161+
$spPassword = ConvertTo-SecureString $credential.SecretText -AsPlainText -Force
162+
$appId = $servicePrincipal.AppId
163+
}
154164
}
155165

156166
return @{

0 commit comments

Comments
 (0)