在现有IE实例打开的选项卡(Open tab in existing IE instance)

2019-07-22 07:47发布

$ie = New-Object -com internetexplorer.application

每次我打开一个新的网站,它是在一个新的IE窗口中打开该对象(即脚本运行时,每一次),我不希望它这样做。 我希望它在新标签中打开,但同样在先前打开的IE窗口。 我想重新使用该对象时,脚本运行下一次。 我不希望创建一个新的对象

那么,有没有办法像检查的Internet Explorer的实例,并重新使用它的实例???

我想这是一个解决方案:

首先,你必须附加到已经运行的Internet Explorer实例:

$ie = (New-Object -COM "Shell.Application").Windows() `
    | ? { $_.Name -eq "Windows Internet Explorer" }

然后你导航到新的URL。 如果这一网址被打开通过Flags参数控制:

$ie.Navigate("http://www.google.com/", 2048)

但我不能够调用navigate方法在这个新创建的对象$ie

Answer 1:

您可以使用Start-Process中打开URL。 如果浏览器窗口已经打开,它将作为一个选项卡中打开。

Start-Process 'http://www.microsoft.com'


Answer 2:

首先,你必须附加到已经运行的Internet Explorer实例:

$ie = (New-Object -ComObject "Shell.Application").Windows() |
      Where-Object { $_.Name -eq "Windows Internet Explorer" }

然后你Navigate到新的URL。 如果这一网址被打开通过控制Flags参数:

$ie.Navigate("http://www.google.com/", 2048)

编辑:在壳体2或多个IE实例正在运行(其他标签算作其他实例为好)枚举将返回一个数组,所以必须选择来自阵列的特定实例:

$ie[0].Navigate("http://www.google.com/", 2048)


Answer 3:

您可以使用此,如果Internet Explorer不是默认的浏览器:

Function Open-IETabs {
    param (
        [string[]]$Url
    )
    begin {
        $Ie = New-Object -ComObject InternetExplorer.Application
    }
    process {
        foreach ($Link in $Url) {
            $Ie.Navigate2($Link, 0x1000)
        }
    }
    end {
        $Ie.Visible = $true
    } 
}

我发现这对PowerShell.com



文章来源: Open tab in existing IE instance