PowerShell Invoke-WebRequest cmdlet and bypassing IE first-launch configuration Error
While setting up a SQL Server job to execute a PowerShell script, I ran into an issue with the service account failing at Invoke-WebRequest cmdlet.
The specific error:
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 D:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\PS-BackupScripts\Backup-SystemDatabases.ps1:80 char:13 + $iam = (Invoke-WebRequest http://xxx.xxx.xxx.xxx/latest/meta-data ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotImplemented: (:) [Invoke-WebRequest], NotSupportedException + FullyQualifiedErrorId : WebCmdletIEDomNotSupportedException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
It works without any errors when I run the script with my credentials as I had launched IE and went past the setup popup. However, the error was indicating that the service account under which the SQL job ran had never opened IE which is the case as we would never log into the server with the service account credentials.
For reference, IE launch screen when opened for the first time
There are 2 ways to fix the issue,
- Use the parameter -UseBasicParsing
According to the documentation, this parameter is required when IE is not installed or configured. However, this parameter is deprecated in PowerShell 6.
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.
The disadvantage would be that all scripts that use Invoke-WebRequest cmdlet would have to be updated and tested.
- Configure Group Policy
Update the group policy to handle the IE’s first run wizard.
Go to Group Policy > Computer Configuration > Policies > Administrative Templates > Windows Components > Internet Explorer
Open “Prevent running First Run Wizard”, set the policy to Enabled and Select your Choice, [Go directly to “Welcome To IE” page] or [Go directly to home page]
I chose the Group Policy update option as it is simpler to make the change in one place rather than editing all PowerShell scripts which would require more testing. And voila, SQL Server job executed perfectly. No more errors!