Governance Group membership addition issue

Hello Experts,

I am getting 400 bad request error while adding members into the governance group. below is the snippet. please help me to fix it

Read CSV

$CsvFilePath=“C:\Users\workgroupmembershipadded.csv”
$groups = Import-Csv -Path $CsvFilePath

foreach ($group in $groups) {
$groupID = $group.GroupID
$MembersName=$group.MembersName
$MemberIdentityID=$group.MemberIdentityID

            #Addming membership
           $memberBody = @(@{ 
                            'type'= "IDENTITY"
                            'id' =  $MemberIdentityID
                            'name'= $MembersName       
                          }) | ConvertTo-Json -Depth 3

                $MembershipURL =  "https://$tenant.api.identitynow.com/beta/workgroups/$groupId/members/bulk-add"
                write-host "MembershipURL" $MembershipURL  
                $AddMembership = Invoke-RestMethod -Uri $MembershipURL -Method POST -Headers $headers -Body $memberBody
                Write-Host -ForegroundColor Green "Added Members to Governance Group: $groupName"
            }

Invoke-RestMethod : {“detailCode”:“400.0 Bad request
syntax”,“trackingId”:“”,“messages”:[{“locale”:“und”,“localeOrigin”:“REQUEST”,“text”:“The request could not be
parsed.”},{“locale”:“en-US”,“localeOrigin”:“DEFAULT”,“text”:“The request could not be parsed.”}],“causes”:}
At line:114 char:38

  • … embership = Invoke-RestMethod -Uri $MembershipURL -Method POST -Heade …
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    • FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Hi @niketnova

can you please check the payload as its giving http 400 bad request error message.

Hi @niketnova ,
Below script will help you to add identities in the Governance Group using CSV file.
Replace **** with required fields like path of csv file, access token and tenant url.

# CSV File Path
$csvPath = "****\workgroupmembershipadded.csv"
# API headers
$headers = @{
    "X-SailPoint-Experimental" = "true"
    "Content-Type" = "application/json"
    "Accept" = "application/json"
    "Authorization" = "Bearer ****"  # Replace with your token
}

# Import CSV and trim whitespace
$csvData = Import-Csv -Path $csvPath | ForEach-Object {
    $_ | ForEach-Object { $_.PSObject.Properties | ForEach-Object { $_.Value = $_.Value.Trim() } }
    $_
}

foreach ($row in $csvData) {

    # Create JSON array exactly like the API expects
    $body = @"
[
  {
    "type": "IDENTITY",
    "id": "$($row.MemberIdentityID)",
    "name": "$($row.MembersName)"
  }
]
"@

    # API URL
    $url = "https://****.api.identitynow-demo.com/v2024/workgroups/$($row.GroupID)/members/bulk-add"

    Write-Host "Adding $($row.MembersName) ($($row.MemberIdentityID)) to Group $($row.GroupID)"

    try {
        $response = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body
        $response | ConvertTo-Json -Depth 5
    }
    catch {
        Write-Host "Error adding $($row.MembersName): $($_.Exception.Response.StatusDescription)"
        $_.Exception.Response | Format-List * -Force
    }
}

Here is csv file headers .
image

Thank you ,

Thanks it’s working as expected. I have an issue with my Body in the script.

Thanks