简体   繁体   中英

How do I access the classic Internet Explorer COM automation object for a running instance of Internet Explorer from Powershell?

How do I access the classic Internet Explorer COM automation object for a running instance of Internet Explorer? That is, if I have Internet Explorer open in multiple windows, how do I associate the COM object corresponding to one of those windows with a variable in Powershell, from within Powershell? The closest I have come to doing this is obtaining the processes "iexplore" and "ieuser" via get-process.

Usually, to obtain access to a COM interface on an existing object, you would use the running object table. Unfortunately, Internet Explorer doesn't register itself with the running object table - but nevertheless, this provides us with some useful Google search results.

For example, Googling "running object table" "internet explorer" found me How to connect to a running instance of Internet Explorer which provides a (VBScript?) sample demonstrating the use of the ShellWindows object.

A quick 'n dirty (no error checking!) translation of this sample to PowerShell script gives us:

$shellapp = New-Object -ComObject "Shell.Application"
$ShellWindows = $shellapp.Windows()
for ($i = 0; $i -lt $ShellWindows.Count; $i++)
{
  if ($ShellWindows.Item($i).FullName -like "*iexplore.exe")
  {
    $ie = $ShellWindows.Item($i)
    break
  }
}
$ie.navigate2("http://stackoverflow.com")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM