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"
$date
Here are two tests to test the normal functionality of Invoke-RestMethod WITHOUT PassThru option
$date = Invoke-RestMethod "http://date.jsontest.com"
$date
Invoke-RestMethod "http://date.jsontest.com" -OutFile "OutFile.txt"
Get-Content "OutFile.txt"
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.