如何通过Powershell为新的Azure AD应用程序配置预授权应用程序



我已经通过Powershell创建了一个新的Azure AD应用程序,但我在配置PreAuthorizedApplication时遇到了问题。该模型包括3个特性,如本文所述。我不使用扩展,这就是为什么我只为AppId和Permissions分配了一个值。但只是为了看看错误的原因,我也给扩展分配了相同的结果,并且总是得到相同的以下错误。

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.

你可以在下面找到我的代码。

$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)

发生此错误是因为-PreAuthorizedApplications的数据类型为System.Collections.Generic.List``1[Microsoft.Open.AzureAD.Model.PreAuthorizedApplication],并且您在Set-AzureADApplication命令中传递了错误的数据类型。你可以做如下代码:

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

最新更新