Credit to all the contributors in the topic Getting Campaign Reports .csv (Follow Up) for helping me put this together.
Couple caveats/dependencies here
-
You need to have the PowerShell SDK installed on your machine
-
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.
-
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)"
}
}