Governance group addition in bulk

Hi Team,
I am using below PowerShell for adding users to governance group but it fails and going to catch block with no error message.
Can someone check and provide me the updated working script.

$baseUrl = "https://ABC-test.api.identitynow.com"
$credential = Import-Clixml -Path "C:\SailPoint\config\ISCSecureCredentials.xml"
$ClientID = $credential.UserName
$SecretID = [System.Net.NetworkCredential]::new('', $credential.Password).Password
$pair = "$($ClientID):$($SecretID)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($pair))
$basicAuth = "Basic $encodedCreds"
$tokenUri = 'https://ABC-test.api.identitynow.com/oauth/token'

$body = @{
    grant_type = 'client_credentials'
 }

$headers = @{
    Authorization = $basicAuth
}

# Request the token
$tokenResponse = Invoke-RestMethod -Uri $tokenUri -Method POST -Headers $headers -Body $body -ContentType 'application/x-www-form-urlencoded'
$token = $tokenResponse.access_token

$csvFile = "E:\Users\input\mapping.csv"
$logFile = "E:\Users\input\bulk_add_log.txt"

$headers = @{
   Authorization  = "Bearer $token"
   "Content-Type" = "application/json"
   "Accept"       = "application/json"
    
}
$data = Import-Csv -Path $csvFile
Write-Host "Loaded $($data.Count) records..."
# Clear old log
"" | Out-File $logFile
foreach ($row in $data) {
   $userId = $row.UserId
   $ggId   = $row.GroupID
   $name   = $row.Name
     $url = "$baseUrl/workgroups/$ggId/members/bulk-add"
     $body = @(
       @{
           type = "IDENTITY"
           id   = $userId
           name = $name
       }
   )
   $jsonBody = $body | ConvertTo-Json -Depth 5
   Write-Host "Adding User: $userId → Group: $ggId"
   try {
       $response = Invoke-RestMethod -Uri $url -Method POST -Headers $headers -Body $jsonBody
       $msg = "SUCCESS: $userId added to $ggId"
       Write-Host $msg
       $msg | Out-File $logFile -Append
   }
   catch {
       $msg = "FAILED: $userId → $ggId"
       Write-Host $msg
       $msg | Out-File $logFile -Append
       if ($_.Exception.Response) {
           $reader = New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())
           $reader.BaseStream.Position = 0
           $reader.DiscardBufferedData()
           $errorResponse = $reader.ReadToEnd()
           Write-Host "Error: $errorResponse"
           "ERROR RESPONSE: $errorResponse" | Out-File $logFile -Append
       }
   }
}
Write-Host "Execution completed. Check log file: $logFile"

Hi @bsayya01

The baseUrl looks incorrect. You are missing the API version. Can you update that and try again and let us know what errors you’re receiving?

Old
$baseUrl = "https://ABC-test.api.identitynow.com"

New:
$baseUrl = "https://ABC-test.api.identitynow.com/v2024"

Hi Tyler,

Thanks for the responce

I did the changes and ran the script

I am getting below error now

Adding User: &&&&&&&&&&&&&&&&&&&&&& → Group: &&&&&&&&&&&&&&&&&&&&&&&&&&&
FAILED: &&&&&&&&&&&&&&&&&&&&&& → &&&&&&&&&&&&&&&&&&&&&&&&&&&
Error: {“detailCode”:“400.0 Bad request syntax”,“trackingId”:“9ae848f18dea4e51b3ca1dc69e7d59b2”,“messages”:[{“locale”:“en-US”,“loc
aleOrigin”:“DEFAULT”,“text”:“The request could not be parsed.”},{“locale”:“und”,“localeOrigin”:“REQUEST”,“text”:“The request could
not be parsed.”}],“causes”:}

My CSV is looking as below

UserId,GroupID,Name
678678867867,678678678-4298-676766-b3f6-a82c094c5870,“Singh, Anish Kumar”

Looks like your body is slightly wrong as well. Try this instead and remove the convert to JSONBody option & body calls. Tested in my own environment and it appears to be working. Also, name is not required in the call, so you can remove that.

$jsonBody = @"
[
  {
    "type": "IDENTITY",
    "id": "$userId"
  }
]
"@

Thanks Tyler.

Your input really helped me.

My script is running now accurately.

Happy to help! If my answer solves your question, please mark it as the solution.