question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invoke-RestMethod parameter combination of OutFile/PassThru does not write to file as intended

See original GitHub issue

Problem 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:

https://github.com/PowerShell/PowerShell/blob/38d582f038f31acfc1a748d60a5331eb82a3bd28/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs#L387

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:open
  • Created 2 years ago
  • Reactions:3
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
iSazonovcommented, May 30, 2021

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:

  1. Write decoded object to pipeline and write decoded object to output file
  2. Write decoded object to pipeline and write raw data to output file
  3. Write raw data to pipeline and write raw data to output file
  4. Write raw data to pipeline and write decoded object to output file

Since there is already buffered stream in one code path any scenario could be implemented.

1reaction
iSazonovcommented, May 28, 2021

When you write to the file, it writes the raw data stream.

Why would we need a raw data? I’d expect we write to file end objects converted to strings.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found