0

Is there a simple command to close the current IE COM Object as part of cleanup?

First attempt, didn't work:

Try{
  $ErrorActionPreference = "Stop"
  $ie = New-Object -COMObject InternetExplorer.Application
  $ie.visible = $true
  $ie.navigate("www.site.com")
  # do other stuff

}
Catch{
  write-host “Exception Message: $($_.Exception.Message)” -ForegroundColor Red

} 

Finally{
  $ie.Quit() # $ie does not have method Quit()
}

Also tried the following:

Catch{
    Get-Process iexplore | ? {$PID -eq $_.Id} | Stop-Process
}

Nothing I do seems to work. All I wish to do is close down the $ie object from the current script. There may be other IE processes running simultaneously, therefore I only want to close the one I just worked with. Any ideas?

Edit: Why is $ie no longer usable by the time the script reaches the Finally{} ? This is not answered in the post you referenced as a duplicate.

Solved, kinda..but not really

$ie = New-Object -COMObject InternetExplorer.Application
$currentIDs = (Get-Process iexplore).Id # how do I only get process IDs of current script?

    Try{
    $ErrorActionPreference = "Stop"

        ... more code ...

    $ie.visible = $true
    $ie.navigate("http://www.allregs.com/tpl/Main.aspx")

        ... more code ...
    }

    Catch{
    write-host “Exception Message: $($_.Exception.Message)” -ForegroundColor Red
    Write-Host "Failed." -ForegroundColor Red 
}

    Finally{
    Get-Process | ? {($_.ProcessName -eq 'iexplore') -and ($_.Id -in $currentIDs )} | Stop-Process
}

This works and closes any IE processes. The issue is that I think this will also close all other IE processes that may be running simultaneously. Can anyone confirm this? And if so, if there's is there a way to only close the current IE processes of this script?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Eric Furspan
  • 742
  • 3
  • 15
  • 36
  • Are you sure `quit()` as mentioned in the answer of the target doesn't clear the $ie variable? – TylerH May 03 '16 at 18:53
  • Not sure what you are referring to when you say 'answer of the target' – Eric Furspan May 03 '16 at 19:12
  • The target is the question that *this* question is marked a duplicate as. So the "answer of the target" is [this](http://stackoverflow.com/a/30643155/2756409). – TylerH May 03 '16 at 19:16
  • Oh okay. Yea, I am unable to even call Quit() on the $ie variable as it says the method doesn't exist. Seems like the entire $ie variable is unworkable after a certain point in the script. Just returns System.ComObject – Eric Furspan May 03 '16 at 19:19

0 Answers0