Powershell Script for Downloading Campaign Reports

Credit to all the contributors in the topic Getting Campaign Reports .csv (Follow Up) for helping me put this together.

Couple caveats/dependencies here

  1. You need to have the PowerShell SDK installed on your machine

  2. At the time of publishing the fixes for 404 when running “Get-CampaignReports” against valid campaign id and Error when attempting to run Start-CampaignReport have been committed but not yet released, so you’ll have to manually modify your module ps1 files locally, but @tyler_mairose’s commits on GitHub will show you what to do.

  3. This script uses the command Get-AccessToken to grab a token used in the Invoke-RestMethod call. This cmdlet will eventually be updated in an upcoming release to Get-IDNAccessToken

Also note that you might not have the same report needs that I do, but you can modify this script to meet your needs. For me, I need a PDF of each campaign report as well as a CSV of the Campaign Status Report, so that’s what my script reflects.

You need the ID of the campaign and a directory you wan them saved to. It will create a folder in that directory with the same title of the campaign.

Here’s the script

param($campaign_id, $output_directory)

Import-Module PSSailPoint

$reportTypes = @("CAMPAIGN_COMPOSITION_REPORT", "CAMPAIGN_REMEDIATION_STATUS_REPORT", "CAMPAIGN_STATUS_REPORT", "CERTIFICATION_SIGNOFF_REPORT")
$campaign_name = (Get-ActiveCampaigns -filters "id eq `"$($campaign_id)`"").name


$file_path = $output_directory + "\" + $campaign_name

if (-not(Test-Path -Path $file_path -PathType Container)) {
    New-Item -Path $file_path -ItemType Directory
}

Write-Host "Beginning report generation for campaign $($campaign_name)"

$token = Get-IDNAccessToken
$baseurl = (Get-DefaultConfiguration).BaseUrl

$headers = @{
    Authorization = "Bearer $($token)"
}

foreach ($reportType in $reportTypes) {
    Start-CampaignReport -Id $campaign_id -Type $reportType
    $report = Get-CampaignReports -Id $campaign_id | Where-Object { $_.reportType -eq $reportType }
    Write-Host "Started refresh of report $($report.name)"
    
    Start-Sleep 20

    $uri = "v3/reports/$($report.id)?fileFormat=pdf"
    $file_name = "$($campaign_name) - $($report.name) - $($report.lastRunAt.ToString("yyyy-MM-dd")).pdf"
    
    Invoke-RestMethod -Method "GET" -Headers $headers -Uri ($baseurl + $uri) -OutFile "$($file_path)\$($file_name)"
    
    if ($reportType -eq "CAMPAIGN_STATUS_REPORT") {
        $uri = "v3/reports/$($report.id)?fileFormat=csv"
        $file_name = "$($campaign_name) - $($report.name) - $($report.lastRunAt.ToString("yyyy-MM-dd")).csv"
    
        Invoke-RestMethod -Method "GET" -Headers $headers -Uri ($baseurl + $uri) -OutFile "$($file_path)\$($file_name)"
    }
}
7 Likes

I am not using this script but we have workato recipe which is using this cc api. I do not see any replacement for this api in depreciation list.

@colin_mckibben is there any replacement available for this api which I am missing.

Yes, there is a replacement, which is documented here

1 Like

Colin beat me to it. I need to update my script

The script has been updated to eliminate the usage of cc API endpoints. Note that I had to use Invoke-RestMethod to get the file instead of Get-Reports due to a quirk in how PowerShell wants to write PDFs to a file. Invoke-RestMethod was required so that I could use the -outfile parameter

“If” it is a ‘quirk’ in PowerShell (and not the PowerShell SDK’s cmdlet), shouldn’t the implementation of the cmdlet have accounted for it to provide an actual viable cmdlet to save to PDF?

I’m not 100% sure where the issue lies. It’s possible either someone on the DevRel side or anyone else who wants to contribute can make a PR or submit a feature request on the GitHub page for the SDK

SailPoint ought to fix / address this, because, as you’ve demonstrated, pulling the PDF report over the API is achievable in PowerShell. And they provide the PowerShell SDK…but failed in this specific capability. The rest of who and how is just fluff. Customers don’t really concern themselves with how SailPoint internally address their own product issues, and shouldn’t need to.