What kind of error Invoke-WebRequest produces then it's always end up in output?

This topic contains 1 reply, has 2 voices, and was last updated by  Curtis Smith 1 year, 10 months ago.

  • Author
  • #54451


    I'm trying to figure out what exactly Invoke-WebRequest cmdlet doing differently then other cmdlets as far as error output is concerned. If you execute it with non-existing URL then you end up with errors being put out into output stream. I assume if I use -ErrorAccount parameter to Silent then it will not show up in stream but it does not work. The same happens if I redirect output to Out-Null. Errors are still end up in a stream. What exactly is happening behind the scenes here?

    Invoke-WebRequest http://www.cnn.com/nonexist.htm -ErrorAction Silent | out-null
    • This topic was modified 1 year, 10 months ago by  GS.
  • #54503

    Curtis Smith

    Hey GS,
    Difference is that invoke-webrequest is throwing a Terminating Error

    You can see this by using Start-Transcript and Stop-Transcript

        Invoke-WebRequest http://www.cnn.com/nonexist.htm
        Get-Content c:\asdfas.fasdf

    If you need to catch a terminating error, use try{} catch{}

        try{Invoke-WebRequest http://www.cnn.com/nonexist.htm}
        catch{"Invoke-WebReqeust returned a terminating error"}
    • This reply was modified 1 year, 10 months ago by  Curtis Smith.

You must be logged in to reply to this topic.