Powershell API calls

Hi,

I’m trying to invoke api calls using Powershell script. could someone help me out on how to get the response headers? I want to get the x-total-count value.

here is the powershell command i use.

$campaignlist=Invoke-RestMethod -Method Get -Uri “https://tenant-sb.api.identitynow.com/beta/campaigns?filters=status eq "ACTIVE”&count=true" -Headers $token1

Hi Chandra,

Quick qualifying question: are you using the official SailPoint PowerShell SDK, or your own?

@chandramohan27 Everything is good except you need to escape the double quotes and alter the token in the header.

There are a few ways to escape the double quotes but one way is to put 2 (“”) like this around sourceName. Also notice the token is being set as the Authorization with a Bearer type. This example also utilizes splatting, where the parameters for the Invoke-RestMethod are set and then passed to that command all at once.

function Get-SourceId {
    param (
        $sourceName,
        $token,
        $org
    )
    $params = @{
        method  = "GET"
        uri     = "https://$org.api.identitynow.com/v3/sources?filters=name eq ""$sourceName"""
        headers = @{
            Authorization = "Bearer $token"
        }
    }
    return (Invoke-RestMethod @params)[0].id
}

In order to get the X-Total-count, you can use PowerShell 7 (technically just 6) as the ResponseHeaderVariable was added. Otherwise you can also use Invoke-WebRequest.


This would look like the following:

$params = @{
    method                  = "POST"
    uri                     = "https://$($org).api.identitynow.com/v3/search?limit=1&count=true"
    body                    = (ConvertTo-Json $body)
    headers                 = @{Authorization = "Bearer $token" }
    ContentType             = "application/json"
    ResponseHeadersVariable = "responseHeader"
}
$response = Invoke-RestMethod @params
[int]($responseHeader."X-Total-Count")[0]

The end result should look something like this:

$campaignlist = Invoke-RestMethod -Method Get -Uri "https://tenant-sb.api.identitynow.com/beta/campaigns?filters=status eq ""ACTIVE""&count=true" -Headers @{Authorization = "Bearer $token1" } -ResponseHeadersVariable "responseHeader"
[int]($responseHeader."X-Total-Count")[0]

3 Likes

@chandramohan27,

If you’d like to use the official SailPoint PowerShell SDK this can be accomplished relatively easily.

See example below:

$Limit = 250
$Offset = 0
$Count=$true
$Filters = 'status eq "ACTIVE"'

# Campaign List
try {
    
    $Result = Get-BetaActiveCampaigns -Limit $Limit -Offset $Offset -Count $Count -Filters $Filters -WithHttpInfo
    Write-Host $Result["Headers"]["x-total-count"]
} catch {
    Write-Host ("Exception occurred when calling Get-BetaActiveCampaigns: {0}" -f $_.ErrorDetails)
    Write-Host ("Response headers: {0}" -f $_.Exception.Response.Headers)
}
3 Likes

@jordan_violet I’m using my own powershell not sailpoint powershell sdk

@ethompson

Thankyou, I tried using invoke-webrequest and i can now get the responseheaders.

Invoke-WebRequest -Method Get -Uri “https://tenant-sb.api.identitynow.com/beta/certifications?filters=campaign.id eq "***********************”&count=true&filters=phase eq "ACTIVE"" -Headers $token1

But i’m getting the results in rawcontent, I need the result body in json

Chandra,

One recommendation would be to use the official SailPoint PowerShell module which solves much of this for you: PowerShell SDK | SailPoint Developer Community

3 Likes

Hi @chandramohan27,

As @jordan_violet suggested, the best and easiest way would be the PowerShell SDK.
While, if you like to stick to your own PowerShell, you can convert the response to JSON using

$response = Invoke-WebRequest -Method Get -Uri $uri -Headers $headers

# Retrieve the JSON response body
$jsonResponse = $response.Content

# You can parse the JSON response body if needed
$parsedResponse = ConvertFrom-Json $jsonResponse

# Convert headers to JSON
$headersJson = $response.Headers | ConvertTo-Json
1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.