AdamGrossTX / ConfigMgr.AdminService

A module for interacting with the ConfigMgr AdminService
MIT License
20 stars 5 forks source link

AdminService.AddMembershipRules #12

Open tehmilcho opened 2 years ago

tehmilcho commented 2 years ago

Hey Mate,

i see in your code that you comment there is a Bug for multiple rules at once. I know it works directly via WMI fine, did you figure out some more? Or is just the "Adminservice" not able in the moment to execute it? Unfortunately to loop through the Clients way to slow if you must add thousands of clients to mutliple collections.

I get in my attempts everytime: {"error":{"code":"404","message":"ODataParameterReader.ReadAsync or ODataParameterReader.Read was called in the \u0027ResourceSet\u0027 state. \u0027CreateResourceSetReader\u0027 must be called in this state, and the created reader must be in the \u0027Completed\u0027 state before the next ODataParameterReader.ReadAsync or ODataParameterReader.Read can be called."}}

mhouston100 commented 1 year ago

I'm having the same issue in my own code. Looping through is crazy slow even for dozens let alone hundreds of rules.

I wonder if anyone has worked this out.

I've put it through to our MS contacts to see if there is some sort of bug or issue.

@tehmilcho are you able to show me an example of the URI and payload? Maybe we can bang our heads together on this.

tehmilcho commented 1 year ago

In this Module is Code for it and the comment that the Autor has the same problem: https://github.com/AdamGrossTX/ConfigMgr.AdminService/blob/main/ConfigMgr.AdminService/docs/Add-CMCollectionRules.md

i opened a case by MS and there told me yes there is an Endpoint but its will not work. But in my experience the MS-Support is pretty bad ... the Call wars open for more then a month also there where pretty clueless what the API is xD There also cant provide me the needed payload.

This Methode for Mutliple is based on the WMI-Class so we use in the moment directly WMI to do this, it works but you need higher permissions as for the API.

https://learn.microsoft.com/de-de/mem/configmgr/develop/reference/core/clients/collections/addmembershiprules-method-in-class-sms_collection

Here are my attempts with the API: (i dont why github is failing to mark as code)

`URI: Invoke-RestMethod -Method POST -Uri https://$SCCMServer/AdminService/wmi/SMS_Collection('$CollectionID')/AdminService.AddMembershipRules -Credential $creds -Body $Body -ContentType "application/json"

Error: {"error":{"code":"404","message":"ODataParameterReader.ReadAsync or ODataParameterReader.Read was called in the \u0027ResourceSet\u0027 state. \u0027CreateResourceSetReader\u0027 must be called in this state, and the created reader must be in the \u0027Completed\u0027 state before the next ODataParameterReader.ReadAsync or ODataParameterReader.Read can be called."}}

tried Payloads:

############################# Variante 1 #################################### $Rules = @() foreach ($Computer in $ComputerList) { $RuleObject = @{

                        "@odata.type"     = "#AdminService.SMS_CollectionRuleDirect"
                        ResourceClassName = 'SMS_R_System'
                        RuleName          =  $Computer.Name
                        ResourceID        =  $Computer.MachineId

                }
$Rules += $RuleObject

}

$BodyRAW = @{ collectionRules = $Rules }

$Body = $BodyRAW | ConvertTo-Json -Depth 100

############################# Variante 2 ####################################

$Rules = @() foreach ($Computer in $ComputerList) { $RuleObject = @{ collectionRule = @{ "@odata.type" = "#AdminService.SMS_CollectionRuleDirect" ResourceClassName = 'SMS_R_System' RuleName = $Computer.Name ResourceID = $Computer.MachineId }

                }
$Rules += $RuleObject

} $BodyRAW = @{ collectionRules = $Rules }

$Body = $BodyRAW | ConvertTo-Json -Depth 100

Payloads as JSON:

{ "collectionRules": [ { "RuleName": "Hostname1", "ResourceClassName": "SMS_R_System", "ResourceID": 16778261, "@odata.type": "#AdminService.SMS_CollectionRuleDirect" }, { "RuleName": "Hostname2", "ResourceClassName": "SMS_R_System", "ResourceID": 16778262, "@odata.type": "#AdminService.SMS_CollectionRuleDirect" }, { "RuleName": " Hostname3", "ResourceClassName": "SMS_R_System", "ResourceID": 16778265, "@odata.type": "#AdminService.SMS_CollectionRuleDirect" } ] }

{ "collectionRules": [ { "collectionRule": { "RuleName": " Hostname1", "ResourceClassName": "SMS_R_System", "ResourceID": 16778261, "@odata.type": "#AdminService.SMS_CollectionRuleDirect" } }, { "collectionRule": { "RuleName": " Hostname2", "ResourceClassName": "SMS_R_System", "ResourceID": 16778262, "@odata.type": "#AdminService.SMS_CollectionRuleDirect" } } ] }

`