2

My case happens when I run $IE.quit() already, but internet explorer still exists in the task manager.

  • if I run the quit function at "quit IE 1" there, the Internet Explorer will close/end (not exits in Task Manager),
  • but when I run it at "quit IE 2" there, the IE not end (still exist in Task Manager)
  • if run with $IE.Visible = $true it has no such problem.
  • Environment: Windows Server 2016, PowerShell v5.1

  1. May I know what may cause this?
  2. May I know after the "confirm page loaded", what happened to $IE? And possibly cause IE not to quit.
  3. Or how I can trace this kind of problem?

I do try to run this without try/catch, but the same thing happens. I try to put $IE = null, but the same, while, the $IE.Quit() description as to force the end of IE, suppose no wonder what is running on the IE. It will end the task.

Here is the PowerShell script:

$looping = 10
timenowhms = (Get-Date -f HH:mm:ss)
try {
    $Url = "http://localhost:8080/commandcenter/checking.aspx"
    $IE = New-Object -Com InternetExplorer.Application
    $IE.Height = 700
    $IE.Width = 750
    $IE.Top = 10
    $IE.Left = 10
    $IE.Visible = $false; # can turn on for testing purpose
    $IE.Navigate2($url);
    $IEPID = [IntPtr]::Zero
    [Win32Api]::GetWindowThreadProcessId($IE.HWND, [ref]$IEPID);
    # quit IE 1
    $IE.Quit();
} catch {
    $timenowhms = (Get-Date -f HH:mm:ss);
    echo "$td_date $timenowhms       Open website failed";
    if ((Get-Process -Name "iexplore*" | ? {$_.Id -eq $IEPID} | measure).Count -eq 1) {
        Stop-Process -Id $IEPID -force
    };
    exit 1
}
# confirm page loaded
while ($ie.Busy -eq $true) {
    if ($looping -eq 0) {
        $timenowhms = (Get-Date -f HH:mm:ss);
        echo "$td_date $timenowhms       Timeout, page not show";
        if ((Get-Process -Name "iexplore*" | ? {$_.Id -eq $IEPID} | measure).Count -eq 1) {
            Stop-Process -Id $IEPID -Force
        };
        exit 1
    } else {
        Start-Sleep -Milliseconds 2500;
        $looping--;
    }
}
# quit IE 2
# $IE.Quit();
exit
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Eng
  • 33
  • 2
  • i could be off, but isn't it `$IE.close()` – Robert Cotterman Aug 13 '19 at 02:25
  • My side get error for this: PS C:\Users\Administrator> $IE.close() Method invocation failed because [System.__ComObject] does not contain a method named 'close'. At line:1 char:1 + $IE.close() + ~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (close:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound – Eng Aug 13 '19 at 02:33
  • I just tried, and mine quits with `$IE.quit()` maybe it's still calculating before quitting, the .quit() method may just be a suggestion to it, and not a kill command. IF it is the only internet explorer open, you could just force it with `get-process -name 'iexplore' | stop-process -force` but this will close all instances – Robert Cotterman Aug 13 '19 at 02:38
  • Yes right away, it didn't close, but after a second i got it to quit with a wait on checking if it exists, you could actually just get a list of current open iexplore commands, then run this, get a new list, and kill any differences – Robert Cotterman Aug 13 '19 at 02:43
  • Want to note, killing the differences doesn't fully work, as some more iexplorer windows will "open" as they are a sub process. But something worth looking into. – Robert Cotterman Aug 13 '19 at 02:51
  • yes. i was get the PID and kill it. but just wondering why the $IE.quit not end task the IE. and i do have $ie.Busy to wait the IE finish process, so suppose not IE still getting in rendering right? – Eng Aug 13 '19 at 02:57
  • Maybe it's the website or something is taking forever, or locking it open, and it's "asking" you to quit... anyhow, found this. might help https://stackoverflow.com/questions/30642883/how-to-properly-close-internet-explorer-when-launched-from-powershell – Robert Cotterman Aug 13 '19 at 03:05
  • Possible duplicate of [How to properly close Internet Explorer when launched from PowerShell?](https://stackoverflow.com/questions/30642883/how-to-properly-close-internet-explorer-when-launched-from-powershell) – Robert Cotterman Aug 13 '19 at 03:08
  • is a good reference, but i just try all that $_.quit(), garbage thing. but same. now the only success way i do is use IE PID to close IE after i get the info. then the script will continue to run and exit. – Eng Aug 13 '19 at 03:53
  • 2
    The 'garbage thing' DOES work. I suspect that by earlier tries you still have a lot of IE Com objects lurking in the background that were not released before. Try with a clean system (i.e. first kill all iexplore processes) and restart your PowerShell instance. – Theo Aug 13 '19 at 09:54
  • Try this: [System.Runtime.Interopservices.Marshal]::ReleaseComObject($IE) | Out-Null – f6a4 Aug 13 '19 at 12:32
  • i do try also. but is same. – Eng Aug 15 '19 at 03:14
  • For Theo: i suspect the same thing. but i do that most time, start with clean also same result.(even i try after restart the server) and $IE.quit suppose force those related IE close right? (like kill ) if it is something running, and IE.quit not force to close it. then how i going to know when the IE is invisible? cause i try with ```visible=true``` it not happen this. – Eng Aug 15 '19 at 03:18

0 Answers0