[Solved] Invoke-WebRequest Response Parsing Error


Sometime back while I was creating a bunch of Azure Automation PowerShell Runbooks for certain Azure Operations automation work, I started getting a strange error from a couple of Runbooks regarding Invoke-WebRequest cmdlet. On further Investigating, I found out the cause and remedy of the same. Let me explain to you in detail about the problem and the solution.

If you try to execute any Azure Automation Runbook, which contains a PowerShell Invoke-WebRequest cmdlet like below:

$WebResponse = Invoke-WebRequest -Uri $url -TimeoutSec 1000 –ErrorAction Stop –Method Post -Headers $headers

You will likely encounter an error stating:

Invoke-WebRequest : 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

You get the above error because the Invoke-WebRequest cmdlet has a dependency on the Internet Explorer assemblies, and invokes them to parse the result as per default behavior.

However, the underlying host on which the Azure Automation Runbook Jobs are executed in the background, do not have IE configured for use by default through the Windows Identity being utilized to execute the Runbooks. We cannot do anything about it since Microsoft does not give end-users any access to the underlying hosts used for executing Azure Automation Jobs.

You may have seen a similar behavior when you spawn a new VM/Machine (with a fresh Windows Server or Client OS Install), and try to run Internet Explorer for the first time. You get a first-launch configuration dialog before you can use Internet Explorer as shown below. Unless you make a choice in this dialog to either select the appropriate option or defer it for later, Internet explorer assemblies are not loaded. This is exactly what also prevents Invoke-WebRequest cmdlet from using those Internet explorer assemblies.

IE First Launch

The right way to make a call to the Invoke-WebRequest cmdlet from within an Azure Automation Runbook would be with the –UseBasicParsing switch as follows:

$WebResponse = Invoke-WebRequest -Uri $url -TimeoutSec 1000 –ErrorAction Stop –Method Post -Headers $headers -UseBasicParsing

Hope you found this blog post useful. If you have any queries/feedback, please feel free to mention in the comments section below.


Be the first to comment

Leave a Reply

Your email address will not be published.