Open URL in existing iexplore identified by PID

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 5 months ago.

  • Author
    Posts
  • #5417

    by Grazza21 at 2013-01-09 08:49:43

    hello,

    I'm a complete novice at PowerShell, but am using PowerShell in preference of VB for the first time to do some IE automation.

    My current issue is attempting to open a new URL in an existing IE browser Window identified by it's Process ID alone. We have tabbed browsing enabled, but iexplore process is limited to one per browser window via a registry setting (i.e. each new browser tab in an IE window does not create a new iexplore process – all tabs in an IE window use the same iexplore process).

    There are plenty of examples where I can open a new tab in an existing window based on the Window Title, but I need the be able to open my new URL in a new Window identified by it's Process ID only. Because Window title cannot be guaranteed unique then deriving Window Tilte from PID as a means of Window Identification is no good for my purposes.

    Here's my current code that first maximizes window based on PID (works OK), but then does not open new URL in same window as required:


    param($param1,$param2)

    $sw = @'
    [DllImport("user32.dll")]
    public static extern int ShowWindow(int hwnd, int nCmdShow);
    '@

    function Act-Window
    {

    $proc = Get-Process | Where-Object {$_.ID -eq "$param1"} | Select-Object Id,Name,MainWindowHandle,MainWindowTitle

    if($proc -ne 0)
    {
    "Activate Win: $Param2"
    $type = Add-Type -Name ShowWindow2 -MemberDefinition $sw -Language CSharpVersion3 -Namespace Utils -PassThru
    $type::ShowWindow($proc.MainWindowHandle, 3)

    "Launch URL in existing browser: $Param3"
    $win = New-Object -comObject Shell.Application
    $ie = @($win.windows() | ? { $_.ID -eq $param1 })[0]
    $ie.Navigate2($param2,2048)
    $ie.visible=$true

    }
    else
    {
    "Launch URL in new browser: $Param3"
    $ie=new-object -com internetexplorer.application
    $ie.Navigate2($param2)
    $ie.visible=$true

    }
    }

    Act-Window

    param1 = The PID of the iexplore process of the browser window I want to send my new URL to.
    param2 = My new URL.

    I need something to replace $ie = @($win.windows() | ? { $_.ID -eq $param1 })[0] which is wrong.

    by DonJ at 2013-01-09 11:19:17

    I'm sorry – I don't think I'm following your question. Is what you're attempting even possible with IE automation? What is it exactly you need to do that isn't working? Get the process ID for the window?

    by Grazza21 at 2013-01-09 12:58:21

    Sorry for not being more clear. In summary, I need to identify an active iexplore process by PID, and then open a new URL in the browser instance running that iexplore process. The PID value will be parsed over to my powershell script by another application and the environment will likely have many iexplore processes running.

    I can get the PID for the window as suggested, but I cannot see a way to then open a new URL in that specific window?

    It's the sort of thing I imagine should be possible? There is a reason for the new URL to be launched in a browser having a specific PID which is too involved to repeat here unless I need to, but I will elaborate if you require.

    Thanks for any assistance.

    by DonJ at 2013-01-09 13:31:58

    No, I don't think you can do that. The IE COM object is designed to launch a new IE window. There are a number of security reasons why you can't be allowed to manipulate an already-open window. There's no way to tell the IE COM object to re-use an existing window.

    It's a bit of an OS/browser thing. For example, on my Win8 VM, if I just invoke a URL using Invoke-Expression, it'll open that URL in a new tab in an existing browser window if there is one. That's a default behavior. I don't get to pick which window, if there are multiples open.

    IE isn't meant to be a fully-controllable component. The COM object gives you good access, but it's intentionally walled off from other processes.

You must be logged in to reply to this topic.