简体   繁体   中英

How to configure PreAuthorizedApplication for a new Azure AD application through Powershell?

I have created a new Azure AD application through Powershell but I have a problem with the configuring PreAuthorizedApplication. The model includes 3 properties as documented here . I don't use the extensions that is why I have assigned a value only for AppId and Permissions. But just to see if the reason for the error, I have assigned to the extensions also but the same result and always gets the same following error.

Set-AzureADApplication : Error occurred while executing SetApplication 
Code: Request_BadRequest
Message: A value without a type name was found and no expected type is available. When the model is specified, each value in the payload must have a type which can be either 
specified in the payload, explicitly by the caller or implicitly inferred from the parent value.

You can find my code below.

$preAuthorizedApplication = New-Object -TypeName "Microsoft.Open.AzureAD.Model.PreAuthorizedApplication"
$preAuthorizedApplication.AppId = $myApiAppRegistrationResultAppId

$preAuthorizedApplicationPermissons = New-Object -TypeName "Microsoft.Open.AzureAD.Model.PreAuthorizedApplicationPermission" 
$preAuthorizedApplicationPermissons.AccessGrants = @($oauth2Permission.Id) # I have tried $oauth2Permission.Value also but nothing changed
$preAuthorizedApplicationPermissons.DirectAccessGrant = "True"
$preAuthorizedApplication.Permissions = $preAuthorizedApplicationPermissons

# Update the Application object
Set-AzureADApplication -ObjectId $appRegistration.ObjectId -PreAuthorizedApplications @($preAuthorizedApplication)

This error occurs because data type of -PreAuthorizedApplications is System.Collections.Generic.List``1[Microsoft.Open.AzureAD.Model.PreAuthorizedApplication] and you are passing wrong data type in Set-AzureADApplication command. You can do something like the code below:

$preAuthorizedApplications = New-Object 'System.Collections.Generic.List[Microsoft.Open.AzureAD.Model.PreAuthorizedApplication]'
$preAuthorizedApplications.Add($preAuthorizedApplication)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM