使用IE的现有实例,除非它包含指定的字符串

时间:2017-09-20 03:02:55

标签: powershell

我使用此代码来确定是否使用IE的现有或新实例:

$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
}

# Elsewhere, I open an array of sites depending on what tabs are already open
foreach ($tab in (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" }) {
    if ($tab.LocationURL.Contains("~"))
    { $search = $true; break }
}

如果打开的标签在其标题中指定了文本,我如何忽略该IE实例,并使用第二个实例打开一系列网站?我试过这个:

$ie = (New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" -and $_.LocationName -like ""}
if ($null -ne $ie) {
    $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
}

2 个答案:

答案 0 :(得分:0)

不确定这是否是您正在寻找的?

$searchString = "something"
$ie = (New-Object -COM 'Shell.Application').Windows() | where {$_.Name -eq 'Internet Explorer' -and $_.LocationName -like "*$searchString*"}
if ($null -ne $ie) {
    # existing instance found
    $newInstance = $false
}
else {
    # create new instance
    $ie = New-Object -COM 'InternetExplorer.Application'
    $newInstance = $true
}

答案 1 :(得分:0)

我有它的工作;改进升值。

$newInstance = $false
if (Get-Process iexplore -ea silentlycontinue | Where-Object {$_.MainWindowTitle -ne ""}) {
    if ((New-Object -COM "Shell.Application").Windows() | ? { $_.Name -eq "Internet Explorer" -and $_.LocationName -notlike "**" }) {
        $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
    }
} else {
    $ie = New-Object -COM "InternetExplorer.Application"
    $newInstance = $true
}