おれさまラボ

実際に手を動かして理解を深めるブログ。

PowerShell:Invoke-WebRequest で Internet Exploler の呼び出しエラーが発生した場合の対処

はじめに

Windows Sandbox 内で PowerShell から Invoke-WebRequest コマンドレットを使って Web アクセスしようとしたところ、見慣れないエラーがでました。

エラーの内容

発生したエラーは以下の通りです。

> Invoke-WebRequest https://www.irasutoya.com/
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.
At line:1 char:1
+ Invoke-WebRequest https://www.irasutoya.com/
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [Invoke-WebRequest], NotSupportedException
    + FullyQualifiedErrorId : WebCmdletIEDomNotSupportedException,Microsoft.PowerShell.Commands.InvokeWebRequestComman
   d

原因

エラー内容を見ると、どうも Invoke-WebRequest コマンドレットは、Internet Explorer に依存するようです。

エラー英文 日本語訳
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. Internet Explorer のエンジンが利用できないため、応答内容を解析できない、またはInternet Explorer の初回起動時の設定が完了していません。UseBasicParsingパラメータを指定して再試行してください。

対処

対処方法は2つあります。

対処方法①:オプション付与で回避する

お手軽な方法としては、UseBasicParsing というオプションを付与することで回避できます。

> Invoke-WebRequest https://www.irasutoya.com/ -UseBasicParsing                           

StatusCode        : 200
StatusDescription : OK
(以下略)

UseBasicParsing オプションについては、Microsofft の公式ドキュメントに以下の記載があります。

"Indicates that the cmdlet uses the response object for HTML content without Document Object Model (DOM) parsing. This parameter is required when Internet Explorer is not installed on the computers, such as on a Server Core installation of a Windows Server operating system." Invoke-WebRequest | Microsoft Docs

ちなみに、PowerShell 6.0.0 以降では、このオプションは非推奨扱いに変更されたようです。つまり、PowerShell 6.0.0 以降を使用している場合は、このエラーが発生することはありません。

"This parameter has been deprecated. Beginning with PowerShell 6.0.0, all Web requests use basic parsing only. This parameter is included for backwards compatibility only and any use of it has no effect on the operation of the cmdlet." Invoke-WebRequest | Microsoft Docs

対処方法②:IE の初回起動時設定を完了させる

Internet Explorer の初回起動時に、以下のような設定画面が出ますので、こちらを完了させます。

おわりに

まっさらな環境だからこそ遭遇するエラーってのは、なかなかお目にかかりづらいですよね。Windows Sandbox のようなイミュータブルな環境をすぐに用意できるようになった恩恵だなぁとしみじみ。

以上