Skip to content

New-MgEntitlementManagementAssignmentPolicy throws error when using ordered hashtable for "choices" element and "localizations" subelement #1983

@brolifen

Description

@brolifen

I'm using ConvertFrom-Json -AsHashtable to convert a json file to a hashtable and feeding this directly to the cmdlet. By design this makes ordered hashtables of all nested elements.
It was a huge pain in the butt to discover the root cause of the error was the ordered hashtable type. But the issue only occurs when choices or localizations is forced to be an ordered hashtable.

Here's an example that works (notice the first element of $questions is cast to be an ordered hashtable:

$accessPackageId = "aca61375-7c6c-4842-910f-1bba4bc7e894"

$localizations = @(
  @{
    text = "France"
    languageCode = "en-US"
  }
)
$choices = @(
  @{
    "@odata.type" = "#microsoft.graph.accessPackageAnswerChoice"
    text          = "France"
    actualValue   = "FR"
    localizations = $localizations
  }
)
$questions = @(
  [ordered]@{
    "@odata.type"              = "#microsoft.graph.accessPackageMultipleChoiceQuestion"
    sequence                   = "1"
    isRequired                 = "true"
    isAnswerEditable           = "true"
    text                       = "What country are you working from?"
    isMultipleSelectionAllowed = "false"
    choices                    = $choices
  }
)

$params = @{
  accessPackage           = @{
    id = $accessPackageId
  }
  displayName             = "A Policy With Questions"
  description             = ""
  allowedTargetScope      = "allMemberUsers"
  expiration              = @{
    type        = "afterDuration"
    endDateTime = $null
    duration    = "P90D"
  }
  requestorSettings       = @{
    enableTargetsToSelfAddAccess    = $true
    enableTargetsToSelfUpdateAccess = $true
    enableTargetsToSelfRemoveAccess = $true
    allowCustomAssignmentSchedule   = $true
  }
  requestApprovalSettings = @{
    isApprovalRequiredForAdd    = $false
    isApprovalRequiredForUpdate = $false
  }
  questions = $questions
}

New-MgEntitlementManagementAssignmentPolicy -BodyParameter $params

Now I also cast the first element of "$choices" as an [ordered] hashtable:

$accessPackageId = "aca61375-7c6c-4842-910f-1bba4bc7e894"

$localizations = @(
  @{
    text = "France"
    languageCode = "en-US"
  }
)
$choices = @(
  [ordered]@{
    "@odata.type" = "#microsoft.graph.accessPackageAnswerChoice"
    text          = "France"
    actualValue   = "FR"
    localizations = $localizations
  }
)
$questions = @(
  [ordered]@{
    "@odata.type"              = "#microsoft.graph.accessPackageMultipleChoiceQuestion"
    sequence                   = "1"
    isRequired                 = "true"
    isAnswerEditable           = "true"
    text                       = "What country are you working from?"
    isMultipleSelectionAllowed = "false"
    choices                    = $choices
  }
)

$params = @{
  accessPackage           = @{
    id = $accessPackageId
  }
  displayName             = "A Policy With Questions"
  description             = ""
  allowedTargetScope      = "allMemberUsers"
  expiration              = @{
    type        = "afterDuration"
    endDateTime = $null
    duration    = "P90D"
  }
  requestorSettings       = @{
    enableTargetsToSelfAddAccess    = $true
    enableTargetsToSelfUpdateAccess = $true
    enableTargetsToSelfRemoveAccess = $true
    allowCustomAssignmentSchedule   = $true
  }
  requestApprovalSettings = @{
    isApprovalRequiredForAdd    = $false
    isApprovalRequiredForUpdate = $false
  }
  questions = $questions
}

New-MgEntitlementManagementAssignmentPolicy -BodyParameter $params

Which throws: Property choices in payload has a value that does not match schema.
The same error is thrown when the elements of "localizations" are cast to [ordered]. It seems the questions section can deal with ordered hashtables just fine but its subelements don't. There might be too strict type checking happening somewhere rejecting the ordered hashtable?

TLDR: You cannot use output from ConvertFrom-Json -AsHashtable directly as some underlying type checking is preventing use of ordered hashtables.

Note: Not sure if this problem is only limited to New-MgEntitlementManagementAssignmentPolicy or more cmdlets. This may will lead many on a merry chase.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions