首页 文章

如何从Powershell访问正在运行的Internet Explorer实例的经典Internet Explorer COM自动化对象?

提问于
浏览
3

如何为正在运行的Internet Explorer实例访问经典Internet Explorer COM自动化对象?也就是说,如果我在多个窗口中打开Internet Explorer,如何在Powershell中将与其中一个窗口对应的COM对象与Powershell中的变量相关联?我最接近这样做的是通过get-process获取进程“iexplore”和“ieuser” .

1 回答

  • 8

    通常,要获取对现有对象的COM接口的访问权限,您将使用运行对象表 . 不幸的是,Internet Explorer没有在运行对象表中注册自己 - 但是,这为我们提供了一些有用的Google搜索结果 .

    例如,Google搜索"running object table" "internet explorer"找到了我How to connect to a running instance of Internet Explorer,它提供了一个(VBScript?)示例,演示了如何使用ShellWindows对象 .

    快速'肮脏(无错误检查!)此示例到PowerShell脚本的转换为我们提供了:

    $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")
    

相关问题