Invoke-RestMethod parameter combination of OutFile/PassThru does not write to file as intended
See original GitHub issueProblem description
In Invoke-RestMethod
, the PassThru parameter must be used with the OutFile parameter. The intent is that you can write the results to the file and get the output to the pipeline. But the cmdlets only write to the pipeline, not the file.
The problem code starts here:
if (ShouldWriteToPipeline)
{
...
}
else if (ShouldSaveToOutFile)
{
...
}
Notice that the logic outputs pipeline and skips the output to the file. Both ShouldWriteToPipeline
and ShouldSaveToOutFile
can be true so need to remove the else
.
Steps to reproduce
Invoke-RestMethod https://taxonomyservice.azurefd.net/taxonomies/product-uri -PassThru -OutFile C:\temp\irm.json
Expected behavior
Output to console and to the file.
Actual behavior
Output to console only. The output file is not created.
Environment data
This affects all versions 5.1 and higher.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:15 (1 by maintainers)
Top Results From Across the Web
Invoke-RestMethod OutFile Empty When PassThru Used
I believe the -PassThru switch redirects all output to the console only, and I think that is why your file is empty.
Read more >Invoke-RestMethod - PowerShell
The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that return richly structured data.
Read more >Working with REST APIs and PowerShell's Invoke- ...
Learn how to work with REST APIs using PowerShell's Invoke-RestMethod cmdlet in this extensive tutorial!
Read more >PowerShell — Ninja PowerShell Blog
The remote code check utilizes Invoke-WebRequest to get the ... This parameter let's you specify the output type, the default is none.
Read more >PowerShell CURL (Invoke-WebRequest) Explained
Gets the content of the web request from a file you specify. The default for this parameter is the current location. 30, OutFile,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It doesn’t seem to make sense to write decoded object to pipeline and raw data to output file.
Then still the question is what kind of behavior do we want:
Since there is already buffered stream in one code path any scenario could be implemented.
Why would we need a raw data? I’d expect we write to file end objects converted to strings.