PowerShell AdminService.AddMembershipRules Error - Unexpected StartArray node was found

Adam D Puls 0 Reputation points
2025-09-05T16:38:47.45+00:00

Hello,

I'm writing a PowerShell script to create multiple direct membership collection rules at one time using the ConfigMgr admin service. When I execute the script, I get an error that says "An unexpected StartArray node was found when reading the JSON Reader". Below is the code I'm using. How am I supposed to add multiple collection rules into one JSON object?

Thanks in Advance!

The full error message is:

Invoke-RestMethod : {"error":{"code":"404","message":"An unexpected \u0027StartArray\u0027 node was found when reading from the JSON reader. A \u0027StartObject\u0027 node was expected."}}

$targetColl = (Invoke-RestMethod -Method Get -UseBasicParsing -Uri "https://$SiteServer/AdminService/v1.0/Collections?`$filter=(CollectionName eq '$TargetCollectionName')" -UseDefaultCredentials).Value
$rules = New-Object System.Collections.Generic.List[psobject]
 foreach ($member in $members) {
        $memberInfo = (Invoke-RestMethod -Uri "https://$SiteServer/AdminService/v1.0/Device?`$filter=Name eq '$($member.Name)'" -UseDefaultCredentials -Method Get).VALUE        
        $rule = @{ 
            "@odata.type"     = "#AdminService.SMS_CollectionRuleDirect"
            ResourceClassName = 'SMS_R_System'
            RuleName          = "$($memberInfo.Name)"
            ResourceID        = $($memberInfo.MachineId) 
        }
       
        $Rules.Add($rule)
    }
    $collectionRules = $rules | ConvertTo-Json -depth 100
$params = @{
        Method                = "Post"
        Uri                   = "https://$SiteServer/AdminService/wmi/SMS_Collection('$($targetColl.SiteID)')/AdminService.AddMembershipRules"
        ContentType           = "application/json"
        UseDefaultCredentials = $true
        Body                  = $collectionRules	
    }
Invoke-RestMethod @params
Microsoft Security | Intune | Configuration Manager | Other
0 comments No comments
{count} votes

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.