PowerShell Get-Report cmdlet write to PDF file

When using Get-Report, it supports -FileFormat of CSV or PDF. In the case of PDF, how do you save the response to a binary file?

Tried the following, but the file isn’t in binary.

$reportID = <someID>
Set-Content Report.pdf -Value $(Get-Report -TaskResultId $reportID -FileFormat pdf)

Note: This question is specific to the usage of the Get-Report cmdlet, less so on calling the REST API to download the report (which there are existing message threads on).

Hi @RandomUser4096 , you can use this cmdlet to convert the PDF

That appears to be a .net method instead of a cmdlet. And it takes a byte array, instead of a response variable, say, from Get-Report.

The specific challenge I’m facing is how to write the PDF file as a binary file. Getting and writing/saving a CSV report doesn’t have the same challenge.

For example the following returns an arraylist object of count 1 into the pdf variable:
get-report -TaskResultId [someId] -FileFormat pdf -OutVariable pdf -ReturnType application/pdf -verbose

Then I do a pdf[0] to get the first element, but at this point, the returned element seems to be in some string / ASCII format, with text like ??? where its content is literally just ??? and not binary anymore.

[byte][char]$pdf.substring(11,1)
[byte][char]$pdf.substring(12,1)
[byte][char]$pdf.substring(13,1)

All returned 63.

Hi @RandomUser4096 This method is used to write byte arrays to files in .NET, which is applicable in PowerShell when handling binary data.

$reportID = "<someID>"
$filePath = "C:\path\to\your\Report.pdf"

Retrieve the report in PDF format
try {
    $pdfContent = Get-Report -TaskResultId $reportID -FileFormat pdf

   Check if the content is valid
    if ($pdfContent -ne $null -and $pdfContent.Length -gt 0) {
        # Save the PDF content to a file
        [System.IO.File]::WriteAllBytes($filePath, $pdfContent)
        Write-Output "Report successfully saved to $filePath"
    } else {
        Write-Output "The report content is empty or null."
    }
} catch {
    Write-Error "An error occurred while retrieving or saving the report: $_"
}

Which version of the PowerShell SDK are you using? Is the returned object type (from Get-Report) on your end different from mine?

Because with your provided script, running on my end, I’m getting a caught exception:

Write-Error: An error occurred while retrieving or saving the report: Cannot convert argument “bytes”, with value: “[the content of the response]”

$pdfContent is a String at this point.

Hi @RandomUser4096 , Apologies for the confusion regarding this matter.

This is the fundamental solution, but it can be improved with additional logic.

$pdfContent = Get-Report -TaskResultId $reportID -FileFormat pdf

$binaryData = [System.IO.File]::ReadAllBytes($pdfContent)

Write-Output “Report successfully saved to $binaryData”

I appreciate your attempts at responding…but none of the above responses has yield a solution nor a step forward unfortunately, and seems to miss the mark each time.

ReadAllBytes reads from a file, not from a string variable:
Opens a binary file, reads the contents of the file into a byte array, and then closes the file.

I’ll re-iterate: Issue is with writing the response from Get-Report to a file as binary data…because the response from Get-Report seems to be string / ASCII(?).

Also, before I started this thread, I’ve tested with getting CSV reports…and able to save them to files without issue. Text / ASCII reports are fine. Issue is specifically with writing / saving binary PDF from the returned data of Get-Report cmdlet.

(When you try to open / view the saved PDF report from Get-Report, it does NOT render correctly…indicating file content issue. If you call the API directly, that report seems fine.)

Hi @RandomUser4096 , that’s good, would it be okay with you to do share the Get-Report file and clean any sensitive information. I can have a look and test it

That’s another challenge it itself…The report (in string) is corrupted, so I can’t securely / correctly scrub sensitive information from it for it to be shared.

Do you not run into the same issue on your end when downloading a PDF report with Get-Report cmdlet? Or are you shooting in the dark with no environment access on your end?

I’m using static files, and this works both ways. However, the Get-Report cmdlet is not available in Microsoft.

So you’ll not see the issue as you’re running in silo without the PowerShell SDK. You need the PSSailPoint module loaded, and link up to a tenant in order to see the issue.

The issue I’m facing is specifically with Get-Report, not how to read / write binary files in PowerShell in the generic sense.

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

@RandomUser4096 I found a solution to this. I too noticed that writing to PDFs was problematic. My workaround was to not use the Get-Reports cmdlet and instead use Invoke-RestMethod so that you can utilize the -outfile parameter.

Here’s a script sample

$report_id = "REPORT_ID_HERE"

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

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

$uri = "v3/reports/$($report_id)?fileFormat=pdf"
    
Invoke-RestMethod -Method "GET" -Headers $headers -Uri ($baseurl + $uri) -OutFile "C:\Temp\campaign_report.pdf"

I updated my full script here

Yeah, the API isn’t the issue. It’s the Get-Report cmdlet not being able to store a usable PDF file.

The PowerShell SDK has been somewhat buggy in my IDN / ISC journey…

You would think there would be a validation step in the test suite of the SDK to verify if the output is actually usable. Or maybe we are the implied automated testers. :rofl: