Search code examples
powershellparameterscomobject

Powershell com object with parameters


I want to write a powershell script that will open an Internet Explorer instance as a com object so i can manipulate it. The problem is I need to run it with a parameter to make sure IE is run as a new session.

The parameter I need to pass is -noframemerging I need to pass this so each instance is a new session. Here is the code I've been using to get the instance started.

$ie = new-object -com "InternetExplorer.Application" 

If someone could give me a hand figuring out how to be able to create multiple objects as a new session in IE that would be fantastic!


Solution

  • Here is my solution:

    $IEProcess = [System.Diagnostics.Process]::Start("iexplore", "-noframemerging")
    Sleep -Seconds 1
    $ie = $(New-Object -ComObject "Shell.Application").Windows() | ? {$_.HWND -eq $IEProcess.MainWindowHandle}
    

    I launch an instance with the System.Diagnostic.Process library, I was using Start-Process but getting some weird outcomes, this fixed that right up. At this point I have an instance of IE open with the noframemerging switch and a variable pointing the process.

    I threw a sleep in there to give the com object a chance to construct before trying to reference it.

    Then I grab all the windows com objects and select the one that has a handle (HWND) that is the same as the one from the process I just started and assign it to my $ie variable.

    The end result is I have a variable that references a com object of Internet Explorer that is independent of all other sessions. The reason I needed this in particular is I'm opening many instances of IE and logging into a website with different user accounts, if they share a session the login page is skipped and they will already be logged in as the first user that logged in.

    Thanks for all the help guys!