Problem using Invoke-Webrequest in a Powershell task on Windows Server

I figured I’d share this one, since it’s one of those fun Microsoft-isms that just like to pop up on a rainy Friday. I had a script that runs on a server, that I had to modify to grab some info off a web page using Invoke-Webrequest. Super simple basic request with no auth. Added it to the script, run the script on the server, works great, life is good!

The next day after the task runs, its obvious the web request failed, and any attempt to feed the direct result of invoke-webrequest into the output to see what was going on also failed, which pretty much tells me that invoke-webrequest is failing.

After doing some research I find that I can use start-transcript to create a log file for the script (definitely going to be leveraging that a bunch in the future), and found that sure enough, invoke-webrequest was failing with the following error:

The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer’s first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.

Thankfully I didn’t slam my head into the keyboard or fly into a rage and throw my laptop or anything (as much as I wanted to), but in this case, I simply ran IE as the user account used on the task, so it would get past it’s “first run” config, and after that, the task ran successfully.

Now, the “proper” fix, apparently specific to Powershell 5.x (and perhaps previous versions), is to use the -usebasicparsing parameter on invoke-webrequest, which bypasses IE. In looking at the documentation though, in 6.x and newer it’s deprecated, since Powershell is cross platform and couldn’t depend on a single browser for all platforms.

From the invoke-webrequest documentation as of August 2021

I also found this discussion on StackExchange that confirms this and has a little discussion about it.

A couple other quick tips I figured I’d mention – using Powershell 5.x and invoke-webrequest as well as start-transcript:

You should add the following line to ensure you’re using TLS 1.2 on Windows Server:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Next, if using start-transcript, make sure the user account you’re running the task as has permission to the file/folder that you specified for the transcript path.

Just think of all the boredom we’d have if every little task wasn’t met with some oddball nuance or dependency. Anyway I hope this helps someone!