Corrupt IE object (IE automation with Powershell)

1.3k Views Asked by At

I want to automate an accesspoint configuration on the web console with Powershell.

I developed on Win8/IE10 and everything works fine when testing the code. Unfortunately I received the "productive system" after development/tests. Now the code should work on Windows Server 2012 R2/IE11.

This is my code for connecting and login to the accesspoint (simplified):

$IE = New-Object -ComObject "InternetExplorer.Application"
$IE.Visible = $true #1#
$IE.Navigate("192.168.0.100")
Write-Host $IE.LocationName

$IE.Document.GetElementByID("login-username").Value = "admin"
$IE.Document.GetElementByID("login-password").Value = "admin"
$IE.Document.GetElementByID("login-btn").Click()
Write-Host $IE.LocationName

For debugging reasons normally I show the window (comment #1#). At first Write-Host, the correct location is shown ("Login"). At second Write-Host, the wrong location is shown ("about:blank"). In the IE window you see the right location/tabname ("AP"). Seems like the IE object become unuseable, other properties (like $IE.Document.*) does not work then. I have to close the window manually.

I have two workarounds:

  1. When I click (get focus) on the IE window manually before login, it works.
  2. When I don't show the windows ($IE.Visible = $false), also everything works.

My scenario/tests so far:

  1. IE ESC is disabled.
  2. I copied the C:\Program Files (x86)\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll from another productive machine (2008R2), so $IE.Document functions would work (dll from my develop machine produces the same error).
  3. I tried to enable/disable protected mode (Internet options > Security) for every zone, does not work for me (found on reddit)

Does someone else have an idea to try? Debugging works with workaround, productive it also works (so far) with unvisible window - but I want to understand the troubles...

Thanks, best regards, kvo

1

There are 1 best solutions below

0
On

I came across this issue once I deployed my scripts to windows server 2012 which runs on IE11.

The cause for this is losing the handler for the IE object created by Powershell.

I used the following snippet, authored in, PowerShell IE9 ComObject has all null properties after navigating to webpage for resolving this issue.

function ConnectIExplorer() {
    param($HWND)

    $objShellApp = New-Object -ComObject Shell.Application 
    try {
      $EA = $ErrorActionPreference; $ErrorActionPreference = 'Stop'
      $objNewIE = $objShellApp.Windows() | ?{$_.HWND -eq $HWND}
      $objNewIE.Visible = $true
    } catch {
      #it may happen, that the Shell.Application does not find the window in a timely-manner, therefore quick-sleep and try again
      Write-Host "Waiting for page to be loaded ..." 
      Start-Sleep -Milliseconds 500
      try {
        $objNewIE = $objShellApp.Windows() | ?{$_.HWND -eq $HWND}
        $objNewIE.Visible = $true
      } catch {
        Write-Host "Could not retreive the -com Object InternetExplorer. Aborting." -ForegroundColor Red
        $objNewIE = $null
      }     
    } finally { 
      $ErrorActionPreference = $EA
      $objShellApp = $null
    }
    return $objNewIE
  } 




$HWND = ($objIE = New-Object -ComObject InternetExplorer.Application).HWND
$objIE.Navigate("https://www.google.com")
$objIE = ConnectIExplorer -HWND $HWND