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"