New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invoke-RestMethod parameter combination of OutFile/PassThru does not write to file as intended #15409
Comments
|
After further investigation, removing the The only "fix" would be to disallow the parameter combination. That would be a breaking change. |
|
Why can't we implement this if we can have Tee-Object? |
|
@iSazonov I suppose we could, but I think that would be a large change to the architecture of the cmdlet. @markekraus may have a better answer. |
|
My inclination would be to write the file and then read the data, perhaps Or, alternatively, can we reset the stream's position after writing to the pipeline and then use it to write the file? |
|
@vexx32 Swapping the order does not change the problem. The problem is that the StreamHelper class does not provide a way to rewind the stream. So the helper class needs to change, which is potentially a lot riskier. EDIT: Oh, I just realized you are suggesting we write the file first then read the file to output to the pipeline. I still prefer having a way to rewind the stream pointer. |
|
Yeah. For small files, we could cache the file in memory as a memory stream before saving/outputting, since those allow us to rewind the stream. To account for large files, it might make sense to use a MemoryStream (or similar) as a buffer (make it say 1024 bytes or whatever) and:
|
I don't see any complicity to add this - there is only two places there we output. And we read all in buffer :-) - BufferingStreamReader - and rewind it with |
|
If we have a large file download -- say, 10gb -- does that still work? Or will the buffering mean that one or both modes of output will only get partial data? Can we afford to completely rewind a 10gb stream or similar and force it to be all kept in memory? Seems to me that we should avoid having the entire stream in memory as much as possible. |
10 Gb for REST? This is not a realistic scenario as far as I'm concerned. Invoke-WebRequest is for downloading large files. |
|
I don't think it will work to try to mix handling of the output together. When you write to the file, it writes the raw data stream. When you write to the pipeline it does a lot of conversion from XML or JSON and creates a PSCustomObject. So pipeline case needs the complete stream before it can transform the data. The real fix to so be able to rewind the stream pointer. When I talked to @markekraus, he didn't seem to think that was possible with the current StreamHelper class. |
Why would we need a raw data? I'd expect we write to file end objects converted to strings. |
|
When we write to a file we write the raw xml or raw json. It does not get converted to objects like when it is written to the pipeline. Look at the code. You will see the difference. |
|
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. |
|
Maintainer review: this is a cmdlet definition/functionality issue - reassigned to Area-Cmdlets-Utility. |
|
The behavior is indeed strange. If you pipe it, you get one string that is unusable. The only way to use the return object is to capture it in a variable then pipe the variable. |
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:
PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/InvokeRestMethodCommand.Common.cs
Line 387 in 38d582f
Notice that the logic outputs pipeline and skips the output to the file. Both
ShouldWriteToPipelineandShouldSaveToOutFilecan be true so need to remove theelse.Steps to reproduce
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.
The text was updated successfully, but these errors were encountered: