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)

$reportTypes = @("CAMPAIGN_COMPOSITION_REPORT", "CAMPAIGN_REMEDIATION_STATUS_REPORT", "CAMPAIGN_STATUS_REPORT", "CERTIFICATION_SIGNOFF_REPORT")
$campaign_name = (Get-Campaign -Id $campaign_id).name
$token = Get-AccessToken
$baseurl = (Get-DefaultConfiguration).BaseUrl

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

$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)"

foreach ($reportType in $reportTypes) {
    Start-CampaignReport -Id $campaign_id -Type $reportType
    $report = Get-CampaignReports -Id $campaign_id | Where-Object { $_.reportType -eq $reportType }
    
    Start-Sleep 20
    Write-Host "Started refresh of report $($report.name)"
    
    $uri = "cc/api/report/get/$($report.id)?format=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 = "cc/api/report/get/$($report.id)?format=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)"
    }
}
6 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