Link to home
Start Free TrialLog in
Avatar of lhutton
lhutton

asked on

Open successive arrays of URLs in IE

I am using this function to open successive arrays of URLs in IE each time the script is run.
  1. If $site is the first or only URL in the array, and IE is not open, then the URL opens in the first tab. On the next run, the next array is opened if $search = $true.
  2. If $site is the first or only URL in the array, and IE is open, then the URL opens in a new tab. QUESTION: On the next run, why is the same array opened?
  3. If $site is not the first or only URL in the array, then the URL opens in a background tab.

function OpenSites($sites) {
    $isFirstSite = $true
    foreach ($site in $sites) {
        if ($isFirstSite -eq $true) {
            if ($newInstance) {
                $ie.Navigate2($site)
            } else {
                $ie.Navigate2($site, $navOpenInNewTab)
            }
        } else {
            $ie.Navigate2($site, $navOpenInBackgroundTab)
        }
        $isFirstSite = $false
    }
}

$today = (get-date).DayOfWeek
switch ($today) { 
    "Someday" {
        $search = $false
        # Check if Google open
        foreach ($tab in $ie) {
            if ($tab.LocationURL.Contains("http://www.google.com/"))
            { $search = $true; break }
        }
        # If Google open on 2nd run, open Yahoo
        if ($search) {
            OpenSites -sites "http://www.yahoo.com/"
        } else {
            # 1st run
            OpenSites -sites "http://www.google.com/ncr","http://www.bing.com/"
        }
    }
}

Open in new window

Avatar of Qlemo
Qlemo
Flag of Germany image

I don't see you changing $newInstance anywhere, meaning it is false all the time?
Avatar of lhutton
lhutton

ASKER

That part of the script:
$newInstance = $false
if (Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""}) {
    $ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" } | Select-Object -First 1
    $newInstance = $false
} else {
    $ie = New-Object -COM "InternetExplorer.Application"
    $newInstance = $true
    $ie.visible = $true
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lhutton

ASKER

Thank you: line 46 fixed the issue. Because I am also running the script on Windows 7 (PS2), I am using:
[code]$navOpenInNewTab = 0x800;
$navOpenInBackgroundTab = 0x1000;[/code]
Otherwise, I do not think I have left out anything pertinent.
You really should consider upgrading to PS 5.1, even on W7 and W2008r2.
Avatar of lhutton

ASKER

If you have a moment, I would greatly appreciate your help with this question, please.