PowerShell AdminService.AddMembershipRules Error - Unexpected StartArray node was found
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