Reputation: 381
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) }
}
}
Upvotes: 0
Views: 892
Reputation: 18950
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"
Upvotes: 1