Skip to content

2.26.1 | In Set-MgUserLicense, null-value key in BodyParameter is omitted unexpectedly #3213

Closed
@Xuzhou-Huang

Description

@Xuzhou-Huang

Describe the bug

I'm using the following script to assign license to users but failed with error:

[Request_BadRequest] : One or more parameters of the operation 'assignLicense' are missing from the request payload. The missing parameters are: removeLicenses.

$sku = Get-MgSubscribedSku -All | Where-Object { $_.SkuPartNumber -eq "SPB_NO_WIN" } 
$skuId = $sku.SkuId 
$params = @{
                addLicenses = @(
                    @{
                        skuId = $skuId
                        disabledPlans = @() 
                    }
                )
                removeLicenses = @() 
            } 

$convertedparams = $params | ConvertTo-Json -Compress 

Set-MgUserLicense -UserId [email protected] -BodyParameter $params -debug

From debug log, I can see the request body get converted to following content where the null-value key is omitted unexpectedly.

Body:
> {
"addLicenses": [
{
"skuId": "e6222d1d-d4a1-4011-97e4-c5f1cb1072f4"
}
]
}

Running the same script with Graph Powershell 2.23.0, it could run successfully. Here's the debug log for your reference where the null-value key is remained as usual.

Body:
{
"addLicenses": [
{
"disabledPlans": [],
"skuId": "e6222d1d-d4a1-4011-97e4-c5f1cb1072f4"
}
],
"removeLicenses": []
}

Expected behavior

Don't omit the null-value key in JSON parameter

How to reproduce

  1. Run script
$sku = Get-MgSubscribedSku -All | Where-Object { $_.SkuPartNumber -eq "SPB_NO_WIN" } 
$skuId = $sku.SkuId 
$params = @{
                addLicenses = @(
                    @{
                        skuId = $skuId
                        disabledPlans = @() 
                    }
                )
                removeLicenses = @() 
            } 

$convertedparams = $params | ConvertTo-Json -Compress 

Set-MgUserLicense -UserId [email protected] -BodyParameter $params -debug
  1. Get error output

Body:
{
"error": {
"code": "Request_BadRequest",
"message": "One or more parameters of the operation 'assignLicense' are missing from the request payload. The missing p
arameters are: removeLicenses.",
"innerError": {
"date": "2025-03-06T06:17:57",
"request-id": "4a20cd18-5923-4c0f-b55e-f8127014304e",
"client-request-id": "fe7ec1c1-5f31-47db-9da8-a51f97e35c6f"
}
}
}

SDK Version

2.26.1

Latest version known to work for scenario above?

2.23.0

Known Workarounds

rollback to 2.23.0

Debug output

Set-MgUserLicense -UserId [email protected] -BodyParameter $params -debug
DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
POST

Absolute Uri:
https://microsoftgraph.chinacloudapi.cn/v1.0/users/xuhuan%40mcpod.partner.onmschina.cn/microsoft.graph.assignLicense

Headers:
FeatureFlag : 00000003
Cache-Control : no-store, no-cache
User-Agent : Mozilla/5.0,(Windows NT 10.0; Microsoft Windows 10.0.19045; en-US),PowerShell/5.1.19041.548
6
SdkVersion : graph-powershell/2.26.1
client-request-id : fe7ec1c1-5f31-47db-9da8-a51f97e35c6f

Body:
{
"addLicenses": [
{
"skuId": "e6222d1d-d4a1-4011-97e4-c5f1cb1072f4"
}
]
}

DEBUG: ============================ HTTP RESPONSE ============================

Status Code:
BadRequest

Headers:
Transfer-Encoding : chunked
Vary : Accept-Encoding
Strict-Transport-Security : max-age=31536000
request-id : 4a20cd18-5923-4c0f-b55e-f8127014304e
client-request-id : fe7ec1c1-5f31-47db-9da8-a51f97e35c6f
x-ms-ags-diagnostic : {"ServerInfo":{"DataCenter":"China North","Slice":"E","Ring":"5","ScaleUnit":"001","RoleIns
tance":"BJ1NEPF00000A4F"}}
Cache-Control : no-cache
Date : Thu, 06 Mar 2025 06:17:56 GMT

Body:
{
"error": {
"code": "Request_BadRequest",
"message": "One or more parameters of the operation 'assignLicense' are missing from the request payload. The missing p
arameters are: removeLicenses.",
"innerError": {
"date": "2025-03-06T06:17:57",
"request-id": "4a20cd18-5923-4c0f-b55e-f8127014304e",
"client-request-id": "fe7ec1c1-5f31-47db-9da8-a51f97e35c6f"
}
}
}

Set-MgUserLicense : One or more parameters of the operation 'assignLicense' are missing from the request payload. The
missing parameters are: removeLicenses.
Status: 400 (BadRequest)
ErrorCode: Request_BadRequest
Date: 2025-03-06T06:17:57
Headers:
Transfer-Encoding : chunked
Vary : Accept-Encoding
Strict-Transport-Security : max-age=31536000
request-id : 4a20cd18-5923-4c0f-b55e-f8127014304e
client-request-id : fe7ec1c1-5f31-47db-9da8-a51f97e35c6f
x-ms-ags-diagnostic : {"ServerInfo":{"DataCenter":"China
North","Slice":"E","Ring":"5","ScaleUnit":"001","RoleInstance":"BJ1NEPF00000A4F"}}
Cache-Control : no-cache
Date : Thu, 06 Mar 2025 06:17:56 GMT
At line:12 char:1

  • Set-MgUserLicense -UserId [email protected] -BodyPara ...
  •   + CategoryInfo          : InvalidOperation: ({ UserId = xuhu...ionJsonSchema }:<>f__AnonymousType0`3) [Set-MgUserLice 
     nse_Assign], Exception
      + FullyQualifiedErrorId : Request_BadRequest,Microsoft.Graph.PowerShell.Cmdlets.SetMgUserLicense_Assign
    

DEBUG: [CmdletEndProcessing]: - Set-MgUserLicense end processing.

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions