@@ -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