Author Posts

September 22, 2016 at 4:33 pm

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, 12 months ago by  GS.

September 23, 2016 at 4:32 am

Hey GS,
Difference is that invoke-webrequest is throwing a Terminating Error
TerminatingError(Invoke-WebRequest)

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

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

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

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