0

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}
3
  • I am disappointed. You more or less just take what you got as answer to your last question, add a new twist to it, and ask again. That's not how this site is supposed to operate. Commented Sep 10, 2017 at 0:00
  • I thought I was being helpful by creating a new question to address a separate, specific issue. Commented Sep 10, 2017 at 0:03
  • But if I close Bing, such that only one tab is open, the script succeeds; how do I need to change that line? Commented Sep 10, 2017 at 0:12

1 Answer 1

0

Your problem seems to be, that the script fails to find (the right) open IE instance on the second run. Debug the Script in the PowerShell ISE (or the PowerShell PowerGUI) and set a breakpoint at $ie=(New-Object -COM "Shell.Application").Windows() | ? {$_.Name -eq "Internet Explorer"}

Run the following command in the interactive PowerShell Window below: (New-Object -COM "Shell.Application").Windows() | Select-Object { $_.Name }

Do you get something like this as output?

File Explorer    
Internet Explorer  
Internet Explorer  
Internet Explorer  
Internet Explorer  

Try to adjust your code as needed when your IE windows have different Names.
The following code works on my machine: I select just the first IE instance (otherwise you get problems since you open multiple sites).

    ...
    # If Google open on second run, open Microsoft and Yahoo
    if ($google) {
        sleep -milliseconds 50
        $ie[0].Navigate2("www.microsoft.com", $navOpenInBackgroundTab);
        sleep -milliseconds 50
        $ie[0].Navigate2("www.yahoo.com", $navOpenInBackgroundTab);
    } else { ...
Sign up to request clarification or add additional context in comments.

1 Comment

I get Windows Explorer as output. If I add | Select-Object -First I get Missing an argument for parameter 'First'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.