scripts/Get-IdentityNowCertCampaignReport.ps1

function Get-IdentityNowCertCampaignReport {
    <#
.SYNOPSIS
    Get IdentityNow Certification Campaign Report(s).
 
.DESCRIPTION
    Get IdentityNow Certification Campaign Report(s).
    Output CSV Reports to file system or return reports as PS Object
    NOTE: Reports are generated on first request. If reports not returned retry after 60 seconds
 
.PARAMETER campaignID
    An IdentityNow Certification Campaign Report(s).
 
.PARAMETER outputPath
    (optional) Report Output Path
    e.g c:\reports
    If omitted CSV Reports returned as PowerShell Object
 
.EXAMPLE
    Get-IdentityNowCertCampaignReport -period 365
 
.EXAMPLE
    Get-IdentityNowCertCampaignReport -period 365 -completed $false
 
.EXAMPLE
    Get-IdentityNowCertCampaignReport -campaignID '2c918085694a507f01694b9fcce6002f'
 
.EXAMPLE
    Get-IdentityNowCertCampaignReport -campaignID '2c918085694a507f01694b9fcce6002f' -outputPath "c:\Reports"
 
.LINK
    http://darrenjrobinson.com/sailpoint-identitynow
 
#>


    [cmdletbinding()]
    param(
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [string]$campaignID,
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [string]$outputPath,
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [string]$period,
        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
        [boolean]$completed = $true
    )
    
    $v3Token = Get-IdentityNowAuth 
    $utime = [int][double]::Parse((Get-Date -UFormat %s))

    $ReportTemplate = [pscustomobject][ordered]@{ 
        ReportName = $null 
        Report     = $null 
    } 

    if ($v3Token.access_token) {
        # Encoded v3 Auth required to get the Reports
        $clientSecretv3 = [System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($IdentityNowConfiguration.v3.Password))
        # Basic Auth
        $Bytesv3 = [System.Text.Encoding]::utf8.GetBytes("$($IdentityNowConfiguration.v3.UserName):$($clientSecretv3)")
        $encodedAuthv3 = [Convert]::ToBase64String($Bytesv3)

        try {            
            if (!$campaignID) {           
                $IDNCertCampaigns = Get-IdentityNowCertCampaign -completed $completed 
                if ($IDNCertCampaigns) {
                    foreach ($campaign in $IDNCertCampaigns) {
                        # Get time of completion of the Campaign
                        $unixtime = $null 
                        [string]$unixtime = $campaign.deadline
                        $unixtime = $unixtime.Substring(0, 10)        
                        $time = [timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($unixtime))

                        if ($time -gt (get-date).AddDays( - $($period))) {
                            $utime = [int][double]::Parse((Get-Date -UFormat %s))
                            $campaignReports = $null     
                            $campaignReports = Invoke-IdentityNowRequest -Method Get -Uri "https://$($IdentityNowConfiguration.orgName).api.identitynow.com/cc/api/campaign/getReports?_dc=$($utime)&campaignId=$($campaign.id)&page=1&start=0&limit=50&sort=%5B%7B%22property%22%3A%22name%22%2C%22direction%22%3A%22ASC%22%7D%5D" -Headers HeadersV3
                            $reportsOut = @()
                            foreach ($report in $campaignReports) {                    
                                if ($outputPath) {
                                    if ($report.name.Equals("Campaign Status Report")) { $outputFile = "$($outputPath)\$($campaign.description) - StatusReport.csv" }
                                    if ($report.name.Equals("Campaign Remediation Status Report")) { $outputFile = "$($outputPath)\$($campaign.description) - RemediationReport.csv" }
                                    if ($report.name.Equals("Certification Signoff Report")) { $outputFile = "$($outputPath)\$($campaign.description) - SignoffReport.csv" }
                                    if ($report.name.Equals("Campaign Composition Report")) { $outputFile = "$($outputPath)\$($campaign.description) - CompositionReport.csv" }
                                }
                                # Get CSV Report
                                $reportDetails = Invoke-RestMethod -Method Get -Uri "https://$($IdentityNowConfiguration.orgName).api.identitynow.com/cc/api/report/get/$($report.taskResultId)?format=csv&name=Export+Campaign+Status+Report&url_signature=$($encodedAuthv3)" -Headers @{Authorization = "$($v3Token.token_type) $($v3Token.access_token)" }                                    

                                if ($outputPath) {
                                    # Output Report to filesystem
                                    $reportDetails | out-file $outputFile                                    
                                }
                                else {
                                    $r = $ReportTemplate.PsObject.Copy()
                                    $r.ReportName = $report.name
                                    $r.Report = $reportDetails | convertfrom-csv 
                                    $reportsOut += $r
                                }
                            }
                            if (-not($outputPath)) { return $reportsOut }
                        }
                    }
                }
                else {
                    write-error "No Certification Campaigns retreived. $($_)"
                }
            }
            else {           
                $utime = [int][double]::Parse((Get-Date -UFormat %s))
                $campaignReports = $null     
                $campaignReports = Invoke-IdentityNowRequest -Method Get -Uri "https://$($IdentityNowConfiguration.orgName).api.identitynow.com/cc/api/campaign/getReports?_dc=$($utime)&campaignId=$($campaignID)&page=1&start=0&limit=50&sort=%5B%7B%22property%22%3A%22name%22%2C%22direction%22%3A%22ASC%22%7D%5D" -Headers HeadersV3 
                    
                if ($campaignReports.count -gt 0) {
                    $reportsOut = @()
                    foreach ($report in $campaignReports) {                    
                        if ($outputPath) {
                            if ($report.name.Equals("Campaign Status Report")) { $outputFile = "$($outputPath)\$($campaign.displayName) - StatusReport.csv" }
                            if ($report.name.Equals("Campaign Remediation Status Report")) { $outputFile = "$($outputPath)\$($campaign.displayName) - RemediationReport.csv" }
                            if ($report.name.Equals("Certification Signoff Report")) { $outputFile = "$($outputPath)\$($campaign.displayName) - SignoffReport.csv" }
                            if ($report.name.Equals("Campaign Composition Report")) { $outputFile = "$($outputPath)\$($campaign.displayName) - CompositionReport.csv" }
                        }
                        
                        # Get CSV Report
                        $reportDetails = Invoke-RestMethod -Method Get -Uri "https://$($IdentityNowConfiguration.orgName).api.identitynow.com/cc/api/report/get/$($report.taskResultId)?format=csv&name=Export+Campaign+Status+Report&url_signature=$($encodedAuthv3)" -Headers @{Authorization = "$($v3Token.token_type) $($v3Token.access_token)" }                                    
                        if ($outputPath) {
                            # Output Report to filesystem
                            $reportDetails | out-file $outputFile                                    
                        }
                        else {
                            $r = $ReportTemplate.PsObject.Copy()
                            $r.ReportName = $report.name
                            $r.Report = $reportDetails | convertfrom-csv 
                            $reportsOut += $r
                        }                                 
                    }
                    if (-not($outputPath)) { return $reportsOut }
                }
                else {
                    Write-Error "Certification Campaign with ID '$($campaignID)'not found."
                }
            }
        }
        catch {
            Write-Error "Failed to retrieve Certifcation Campaigns. $($_)" 
        }
    }
    else {
        Write-Error "Authentication Failed. Check your AdminCredential and v3 API ClientID and ClientSecret. $($_)"
        return $v3Token
    }
}

# SIG # Begin signature block
# MIINSwYJKoZIhvcNAQcCoIINPDCCDTgCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUankT9YekiHYnd5qqAGWuUo5w
# 1J6gggqNMIIFMDCCBBigAwIBAgIQBAkYG1/Vu2Z1U0O1b5VQCDANBgkqhkiG9w0B
# AQsFADBlMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYD
# VQQLExB3d3cuZGlnaWNlcnQuY29tMSQwIgYDVQQDExtEaWdpQ2VydCBBc3N1cmVk
# IElEIFJvb3QgQ0EwHhcNMTMxMDIyMTIwMDAwWhcNMjgxMDIyMTIwMDAwWjByMQsw
# CQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3d3cu
# ZGlnaWNlcnQuY29tMTEwLwYDVQQDEyhEaWdpQ2VydCBTSEEyIEFzc3VyZWQgSUQg
# Q29kZSBTaWduaW5nIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA
# +NOzHH8OEa9ndwfTCzFJGc/Q+0WZsTrbRPV/5aid2zLXcep2nQUut4/6kkPApfmJ
# 1DcZ17aq8JyGpdglrA55KDp+6dFn08b7KSfH03sjlOSRI5aQd4L5oYQjZhJUM1B0
# sSgmuyRpwsJS8hRniolF1C2ho+mILCCVrhxKhwjfDPXiTWAYvqrEsq5wMWYzcT6s
# cKKrzn/pfMuSoeU7MRzP6vIK5Fe7SrXpdOYr/mzLfnQ5Ng2Q7+S1TqSp6moKq4Tz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