Api script to add user to group

Hi,

We're writing some reports the GET function is working fine, but when we make POST to add new user to group we get error 400, can someone point me at right direction or tell me what i'm doing wrong, with same script just modified the body part i can create the user, but then i cant put in a group.

after authenticate this is the relevant script lines

$headersX = @{
'New-Object' = 'System.Collections.Generic.Dictionary[[String],[String]]'
'X-Tenant-ID' = 'tenantid'
'Authorization' = 'Bearer $res'
'Accept' = 'application/json'
'Content-Type' = 'application/json'
}

############################

$body.Add("ids" , "userid")

$data = $body | ConvertTo-Json

##########################

Invoke-RestMethod -Uri 'url api/common/v1/directory/user-groups/griupid/users' -Method POST -Body $data -Headers $headersX

return error 400 bad request, but with same script i can create new user

Parents
  • The body must only have the user IDs. Store the groupID in a separate variable.
    Example

    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("X-Tenant-ID", "tenantidstring")
    $headers.Add("Content-Type", "application/json")
    $headers.Add("Authorization", "Bearer tokenstring")

    $body = "{`n    `"ids`": [`n        `"userid`"`n    ]`n}"

    $response = Invoke-RestMethod '$data-region/common/v1/directory/user-groups/groupID/users' -Method 'POST' -Headers $headers -Body $body
    $response | ConvertTo-Json

  • Thanks Ismall, yeah i realized the problem come from ad since the users i try to put into this group are from there, until api is capable of that isnt useful at all for this kind of automation.

    btw the script works if the user is created inside sophos directory.

Reply Children
No Data