Looking to terminate IE COM Object browser sessions at the end of my script, in my Finally{} statement.
I would think it be as simple as:
Finally{
$ie.Quit()
}
But its not. the $ie object is unreachable at this stage in the script and no methods can be called on it. Anyone know why this is? Did the $ie object go out of scope?
I have defined the $ie object earlier, before my Try{} statement:
$ie = New-Object -COMObject InternetExplorer.Application
Try{...}
But I dont actually Navigate() until inside the Try{} statement:
$ie = New-Object -COMObject InternetExplorer.Application
Try{
$ie.navigate("http://www.allregs.com/tpl/Main.aspx")
}
Then I have my catch{} statement:
Catch{
write-host “Exception Message: $($_.Exception.Message)” -ForegroundColor Red
}
And lastly my Finally{} statement:
Finally{
$ie.Quit() # 'can't call method on System.ComObject.' Null?
}
Any ideas why I can't Quit() the current $ie process at this stage in the script? Am I missing something? Is there another way to end the current Internet explorer session, without closing ALL of them? Any input welcome. Thanks.
Update:
So it appears $IE is unreachable at the end because the code never reaches the part where $ie actually navigates and gets a value. It only gets initialized at the start. In fact, it appears my Invoke-Webrequest requests (which log me in) are what prompt IE to start up and this is why a call to $ie.Quit() does nothing. My question now, is there a way to close IE sessions started using Invoke-Webrequest? -UseBasicParameter works to supress IE from starting up but it also messes up my code in weird ways so this doesn't seem like an option