0

I have created a function that opens an array of one or more URLs in IE: the first URL should be opened in the first tab; successive URLs should be opened in background tabs. If IE is already open, the first URL should be opened in a new tab to preserve existing URLs.

function OpenSites($sites) {
    $isFirstSite = $true
    foreach ($site in $sites) {
        if ($isFirstSite) {
            if (Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""}) {
                $ie.Navigate2($site, $navOpenInNewTab)
            } else { $ie.Navigate2($site) }
            $isFirstSite = $false
        } else { $ie.Navigate2($site, $navOpenInBackgroundTab) }
    }
}
3
  • You could wrap it in an if statement as linked here: stackoverflow.com/questions/16356098/…. if ( Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""} ) Commented Sep 17, 2017 at 2:31
  • I adjusted my question in response to your comment. Why is an empty first tab opened if IE is not already open? Commented Sep 17, 2017 at 4:03
  • The part where you get/create the $ie instance is missing. You should always provide a Minimal, Complete, and Verifiable example. Commented Sep 17, 2017 at 4:56

1 Answer 1

1

I guess the first URL should be opened only in the first tab if the IE was not started before? And all successive URLs should be opened in background tabs, right?

Update: I hope this code is what you want:

$navOpenInNewTab = 0x800;
$navOpenInBackgroundTab = 0x1000;

$ie = $null
$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
    sleep -milliseconds 500
    $ie.visible = $true
    $newInstance = $false
} else {
    $ie = New-Object -COM "InternetExplorer.Application"
    sleep -milliseconds 500
    $ie.visible = $true
    $newInstance = $true
}
#[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
#[Microsoft.VisualBasic.Interaction]::AppActivate("$($ie.LocationName) $($ie.Name)")
    sleep -milliseconds 50

Write-Host $newInstance
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
    }
}

OpenSites -sites "www.bing.com","www.heise.com"
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you; as you might have guessed I am trying to make the script scalable by introducing functions.................... If IE is already open on the 1st run, both OpenSites URLs open in background tabs. On the 2nd run, the test for whether a URL with a specified string is open is skipped, and the same OpenSites URLs are opened again.................... If IE is not already open on the 1st run, the OpenSites URLs open as expected. On the 2nd run, the test succeeds, but the next OpenSites URLs are opened in background tabs.
(1) No: the first or only OpenSites URL should open in a new tab. (2) I have the beginnings of a 2nd function, but am otherwise using the test here. (3) As per (1).
Okay, I think the update should do the trick now. I have noticed the script behaved differently when it was executed from the shell instead of the PoSH ISE. Strange.
Almost there: if IE is already open, on the 2nd run the test is skipped, and the same OpenSites URLs are opened again.
This is not part of the original post. I guess I should stop to "work" for you. First, I had the impression you stepped up, but now..
|

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.