Invoke-RestMethod OutFile Empty When Using PassThru

Using the PowerShell 4.0 cmdlet and Invoke-RestMethod. I have problems with the -OutFile and -PassThru options. Whenever I add the -PassThru parameter, my -OutFile is created, but the contents are Empty!

According to the Invoke-RestMethod Documentation, both the output file and the pipeline object should be available when these parameters are used together. "-OutFile Saves the response body in the specified output file. [...] To send the results to the file and to the pipeline, use the Passthru parameter."

Here is a test to repeat the problem I have . Here I call rest api, trying to BOTH save the response to the AND deserialize file into a powershell object.

"POWERSHELL VERSION $($host.Version.ToString())"
$date = Invoke-RestMethod "http://date.jsontest.com" -OutFile "OutFile.txt" -PassThru
Get-Content "OutFile.txt"
# FILE IS EMPTY!!! PASSTHRU SEEMS TO RESULT IN EMPTY FILE
$date
# powershell object has the date received from api

Here are two tests to test the normal functionality of Invoke-RestMethod WITHOUT PassThru option

# ... Test # 1, call rest api and deserialize into powershell object
$date = Invoke-RestMethod "http://date.jsontest.com"
$date
# Output shows the date retrieved from sample restful service
# ... Test # 2, call rest api and save response body directly to a file
Invoke-RestMethod "http://date.jsontest.com" -OutFile "OutFile.txt"
Get-Content "OutFile.txt"
# Output shows contents of rest api response body (json text)

I think these tests will help others see the problems that I have. My question is, is there something that I am missing to do this work, or could it be a bug with the cmdlet? . I was looking a bit for a solution and no obvious posts about this problem. I want to use -OutFile as part of a workaround for another Invoke-RestMethod content encoding issue as described in Bug? Invoke-RestMethod and UTF-8 data . The -PassThru option is useful for viewing feedback data and completing an iteration in a multitask (paged) set of ode results.

+6

Source: https://habr.com/ru/post/1016017/


All Articles