On the second run of this script, I can open Microsoft and Yahoo if I first close Bing; how can I open those sites without first closing Bing? The error:
Method invocation failed because [System.Object[]] doesn't contain a method named 'Navigate2'.
+ $ie.Navigate2 <<<< ("www.microsoft.com", $navOpenInNewTab);
+ CategoryInfo : InvalidOperation: (Navigate2:String) [], Runtime Exception
+ FullyQualifiedErrorId : MethodNotFound
Why does it fail when I have Bing and Google open, but not when I have Google open?
# Set BrowserNavConstants to open URL in new tab
# Full list of BrowserNavConstants: https://msdn.microsoft.com/en-us/library/aa768360.aspx
$navOpenInNewTab = 0x800;
$navOpenInBackgroundTab = 0x1000;
$ie = $null
if (Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""}) {
$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" }
} else {
$ie = New-Object -COM "InternetExplorer.Application"
sleep -milliseconds 50
$ie.visible = $true
}
$today = (get-date).DayOfWeek
switch ($today) {
"Someday" {
$ie.Navigate("www.bing.com");
$ie.Navigate2("www.yahoo.com", $navOpenInBackgroundTab); break
}
default {
$google = $false
# Check if Google open
foreach ($tab in $ie) {
if ($tab.LocationURL.Contains("google"))
{ $google = $true; break }
}
# If Google open on second run, open Microsoft and Yahoo
if ($google) {
$ie.Navigate2("www.microsoft.com", $navOpenInNewTab);
$ie.Navigate2("www.yahoo.com", $navOpenInBackgroundTab);
} else {
# On first run, open Bing and Google
$ie.Navigate("www.bing.com");
$ie.Navigate2("www.google.com", $navOpenInBackgroundTab);
}
break
}
}
# Cleanup
'ie' | ForEach-Object {Remove-Variable $_ -Force}