9

I've got a REST call (POST) for downloading a PDF file. The API call responds in a binary version of the PDF file. I want to save the file on my machine as a .pdf. However, I am getting an error:

Out-File : Access to the path 'C:\Users\my-name\Documents\Tests' is denied.
At C:\Users\my-name\Documents\Tests\TEST-WITH-LOOP.ps1:132 char:149
+ ... questBody | Out-File -FilePath ("C:\Users\my-name\Documents\Tests
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Out-File], UnauthorizedAccessException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand

Response body header from POSTMAN:

Content-Disposition →attachment; filename="Report-01_29_14-28.pdf"
Content-Type →application/octet-stream
DataServiceVersion →2.0
Date →Mon, 29 Jan 2018 21:51:31 GMT
Date →Mon, 29 Jan 2018 21:51:31 GMT
Server →Jetty(9.3.7.v20160115NeotysEdition.30)
Transfer-Encoding →chunked

Command:

$DownloadReportResponse = Invoke-RestMethod -Method Post -Uri $DownloadReportUrl -ContentType "application/json" -Body $DownloadReportRequestBody |
    Out-File -FilePath ("C:\Users\my-name\Documents\Tests") -Force

Question: How do I save this to my machine as a .pdf or possibly include as an attachment to the email?

4
  • Isn't the error clear enough? You miss file access rights at folder "C:\Users\my-name\Documents\Tests". Commented Jan 29, 2018 at 23:11
  • So what is the solution to have the rights for file access? I am running this script as an Administrator and have the Execution policy set to RemoteSigned.
    – Shahboz
    Commented Jan 29, 2018 at 23:18
  • Does the used account have even access rights to that folder? Being an admin doesn't grant folder rights to user-specific folders under Windows. Check that. Easier would be to use another (possibly more appropriate) folder, even if it was for testing only so overcome the issue. Commented Jan 29, 2018 at 23:20
  • I don't know how to get the information you're suggesting. But I created a new folder in C: directory and no luck. Same error. Maybe the script is looking for the file in the directory and cannot find one?
    – Shahboz
    Commented Jan 29, 2018 at 23:27

3 Answers 3

15

Well, the error is a bit misleading. You're trying to create the output file as C:\Users\my-name\Documents\Tests, but that already exists as a directory, hence the access violation. Create the file with the full path:

Invoke-RestMethod ... |
    Out-File "C:\Users\my-name\Documents\Tests\Report-01_29_14-28.pdf"
8
  • Thanks Ansgar, that solved the issue. But when I open the saved file I'm getting an error saying it's not the right pdf file. Maybe there's an issue with decoding the binary file? What do you think? Is there a way to decode the binary pdf file in powershell?
    – Shahboz
    Commented Jan 30, 2018 at 0:15
  • Depends on what ouput your Invoke-RestMethod call actually produces. Capture it in a variable (without piping into Out-File) and check the type: $rsp = Invoke-RestMethod ...; $rsp.GetType().FullName; $rsp | Get-Member. Commented Jan 30, 2018 at 8:25
  • $DownloadReportResponse.GetType(); $DownloadReportResponse | Get-Member is this the right command? Because I am getting You cannot call a method on a null-valued expression. error
    – Shahboz
    Commented Jan 30, 2018 at 18:31
  • You did remove the | Out-File ... from the Invoke-RestMethod call like I told you, didn't you? Commented Jan 30, 2018 at 19:17
  • I did, here is my call. $DownloadReportResponse = Invoke-RestMethod -Method Post -Uri $DownloadReportUrl -ContentType "application/json" -Headers $headers -Body $DownloadReportRequestBody
    – Shahboz
    Commented Jan 30, 2018 at 19:30
2

I had this same concern when using the Out-File command to redirect output to a file.

My command was this:

Out-File -FilePath C:\MyFiles -InputObject "My text"

When I run the command I get this error:

Out-File: Access to the path 'C:\MyFiles' is denied.

The issue was that I was redirecting the output ("My text") to a directory and not a file.

I had to modify it this way by specifying the file (program.txt):

Out-File -FilePath C:\MyFiles\program.txt -InputObject "My text"

This time it worked out fine.

That's all.

I hope this helps

0

Removing read-only attribute for a file helped for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.