Managing IE

Tagged: 

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Anthony Baron Anthony Baron 4 months, 2 weeks ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #38956
    Profile photo of Anthony Baron
    Anthony Baron
    Participant

    I need to use PowerShell to capture the F12 application code/logs for diagnosis purposes from IE and can easily do this with the following code:

    >> $oIE=new-object -com internetexplorer.application
    >> $oIE.Visible = $true
    >> $oIE.navigate2("microsoft.com")
    >> $oIE.Document.documentElement | out-file -filepath("c:\users\antho\desktop\IE.txt")

    But I then need to be able to do this on a system where I did not start IE, and need to find which browser is pointing to the site I am interested in.

    I know I can find IE with the following:

    PS C:\Windows\system32> get-process iexplore

    Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id SI ProcessNa
    ——- —— —– —– —– —— — — ———
    634 43 13524 43236 ...53 4.13 12424 4 iexplore
    824 54 53824 71488 474 243.27 74916 4 iexplore
    934 59 76052 96608 491 98.11 131556 4 iexplore

    However I then want to be able to test each iexplore to see which one is running the web session I am interested in.

    I know that I can use $oIE.document.documentElement from above, but what I don't know is how to do this with the three iexplore sessions that I just identified. Ideally I want to be able to test each of the domains or web address and then grab the .documentElement for that browser.

    I have spent a couple of days trying to crack this I seem to be running in circles.

    Any pointers here would be fabulous.

    #38959
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    It's a tricky problem. The System.Runtime.InteropServices.Marshal class has a GetActiveObject() method ( https://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getactiveobject%28v=vs.100%29.aspx ), but it will only return the first instance found. COM has something called the running object table that you can use to manage multiple instances, but there's no easy way to do that built into .NET already, and a quick web search seems to indicate that people are having problems with the ROT giving you back the same instance when they're trying to do something similar to what you're describing: http://adndevblog.typepad.com/autocad/2013/12/accessing-com-applications-from-the-running-object-table.html

    #38973
    Profile photo of Anthony Baron
    Anthony Baron
    Participant

    A follow-on question

    1. Does the HWND window handle of the l current Internet Explorer window match any of the properties in get-process.

    Thanks

    #38988
    Profile photo of random commandline
    random commandline
    Participant

    Either option should work for you, but I noticed the size of each text file was inconsistent when testing each address multiple times.

    # ComObject Method
    $addresses = "www.microsoft.com
    www.google.com
    www.powershell.org" -split "`n"
    
    # Get all open shell windows
    $app = New-Object -ComObject 'shell.application'
    
    # Export each documentElement
    foreach ($address in $addresses){
        $ie = New-Object -ComObject 'InternetExplorer.Application'
        $ie.navigate($address)
        $ie.visible = $true
        while ($ie.Busy){Start-Sleep -Seconds 2}
        [void]($address -match "\.(?'name'.*)\.") ; $name = $Matches['name']
        $window = $app.windows() | Where-Object {
            $_.Name -eq 'Internet Explorer' -AND $_.LocationURL -like "*$name*"}
        $window.Document.documentElement | Out-File ".\Desktop\test\$address.txt"
        $window.Quit()
    }
    
    # Invoke-WebRequest Method
    foreach ($address in $addresses){
        $useragent = '([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer)'
        $request = Invoke-WebRequest -Uri $address -UserAgent $useragent
        $request.ParsedHtml.documentElement | Out-File ".\Desktop\test\$address.txt"
    
    $app = New-Object -ComObject 'shell.application'
    $handles = (Get-Process -Name iexplore).MainWindowHandle
    foreach ($handle in $handles){
       $window = $app.windows() | Where-Object {$_.HWND -eq $handle}
       $window.Document.documentElement | Out-File ".\Desktop\test\$handle.txt"
    }
    
    #39010
    Profile photo of Anthony Baron
    Anthony Baron
    Participant

    Thanks for the direction so far.
    I need to identify each instance of Internet Explorer already running and not start new sessions.
    I am doing this via
    $Test1 = ( get-process | Where-Object {$_.ProcessName -Eq 'iexplore'} | select -ExpandProperty mainWindowHandle )

    And this gives me a list of windows handles:
    396408
    199978
    330736
    789426

    What I then need to do is to get access to the IE document properties such as:
    $ie.document.documentElement
    At the moment I only know how to do this when you create a new-object:
    $ie = New-Object -ComObject 'InternetExplorer.Application'
    However, I need to do this for an existing object that I have identified via the windows handle as above.
    Thanks 

    #39017
    Profile photo of random commandline
    random commandline
    Participant

    Try the third option on my previous post. Some of the handles and HWND are identical.

    #39022
    Profile photo of Anthony Baron
    Anthony Baron
    Participant

    Thanks this works, I appreciate the help 🙂

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.