Invoke-WebRequest - The Response Content Cannot Be Parsed

Introduction

I recently faced the above issue while running the PowerShell script. This script has the following line of code and throws the below error. The script has been running fine for the past few months.

$CheckConnection=Invoke-WebRequest-Uri$urlparam

Invoke-WebRequest : The response content cannot be parsed

Invoke-WebRequest: The response content cannot be parsed because the internet explorer engine is unavailable or Internet Explorer’s first-launch configuration is incomplete. Specify the UseBasicParsing and try again.

Description

The invoke-WebRequest is introduced in PowerShell 3.0 to parse the HTML response from the API request response. In this script, I used this command to check whether the account has access to a specific site URL in SharePoint online.

The command invoke-WebRequest sends the request to the URL, parses the response, and returns a collection of images, links, and other significant elements, such as the Status Code and Status Description.

In this case, what I was doing was checking the response collection. If the status code is 200, which is ‘Ok,’ then I proceed to the next steps. If anything, other than 200, the script smoothly closes the operation by writing an error message to log files.

Due to recent changes from the MSFT where IE is deprecated and the latest OS and security patches redirects all IE based requests to Microsoft Edge, the script started throwing error and is giving a message saying that “The response content is not parsed since the Internet Explorer engine is not available’. The reason is that the command invoke-WebRequest uses IE COM API to parse the document. Since the IE engine is not available, it is throwing this error. Once the latest patches are pushed to our work VDIs, the script command Invoke-WebRequest stops working in this scenario.

Fix

To overcome this issue, it is necessary to update by adding the switch -UseBasicParsing. This command with switch uses basic REGEX based parsing instead of calling IE COM API, which will work where IE is not available or installed.

$CheckConnection=Invoke-WebRequest-Uri$urlparam-UseBasicParsing

References