IE导航到本地文件时,无法根据HWND找到iexplorer进程?

Cannot find iexplorer process based on HWND when IE is navigated to a local file?

我正在尝试查找 Internet Explorer com 对象的进程,我在下面提供的代码在 IE com 对象 像 google.nl 一样被导航到 URL,但是当它被导航到本地 html 文件时它不会。

关于函数的一些信息:

  1. 它创建一个新的 ieplorer com 对象
  2. 它更改了 iexporer com 对象的一些属性。
  3. 它使用带有指定 url
  4. 的 ieplorer com 对象的导航功能
  5. 它 return 基于 HWND 的此 com 对象的进程对象。

问题存在于第 4 步,很简单 where 逻辑:

Get-Process -Name iexplore | Where-Object {$_.MainWindowHandle -eq $ie.HWND}

我尝试了几种方法,甚至延迟构建,但不知何故它不起作用。 如果通过直接指定 HWND 从函数外部尝试逻辑,它确实有效,例如:

PS C:\> Start-IELockedDown localhost
HWND = 659256
HWND Type = int
IE Processes:
@{Name=iexplore; MainWindowHandle=919956}
@{Name=iexplore; MainWindowHandle=397090}
@{Name=iexplore; MainWindowHandle=659256} #matching Entry
@{Name=iexplore; MainWindowHandle=0}
@{Name=iexplore; MainWindowHandle=69090}
@{Name=iexplore; MainWindowHandle=0}
IE with correct HWND
#Nothing?
After this comes the RETURN:
#Also Nothing!

PS C:\> Get-Process -Name iexplore | Where-Object {$_.MainWindowHandle -eq 659256}

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    582      42     9656      30940       0,50  10260   2 iexplore

调试示例:

1 当我 运行 使用像这样的外部和工作网站命令时 Start-IELockedDown google.nl 它实际上有效:

HWND = 921524
IE Processes:
@{Name=iexplore; MainWindowHandle=921524} #Matching entry
@{Name=iexplore; MainWindowHandle=724706}
@{Name=iexplore; MainWindowHandle=69090}
@{Name=iexplore; MainWindowHandle=0}
IE with correct HWND
@{Name=iexplore; MainWindowHandle=921524} #Logic works
After this comes the RETURN:

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    480      31     7092      26848       0,22  18912   2 iexplore

2 当我使用文件路径而不是像这样的外部网站时 Start-IELockedDown (Get-Item .\blank-page.html | Select-Object -ExpandProperty FullName) 逻辑似乎中断了:

HWND = 3015698
IE Processes:
@{Name=iexplore; MainWindowHandle=0}
@{Name=iexplore; MainWindowHandle=3015698} #Matching Entry
@{Name=iexplore; MainWindowHandle=1248958}
@{Name=iexplore; MainWindowHandle=69090}
@{Name=iexplore; MainWindowHandle=0}
IE with correct HWND
#Nothing?
After this comes the RETURN:
#Also nothing!

3 当我 运行 它针对 localhost 时没有响应,因为我没有像这样的本地网络服务器 运行ning Start-IELockedDown LocalHost 它给了我同样的问题:

HWND = 986890
IE Processes:
@{Name=iexplore; MainWindowHandle=986890} #Matching Entry
@{Name=iexplore; MainWindowHandle=1051034}
@{Name=iexplore; MainWindowHandle=69090}
@{Name=iexplore; MainWindowHandle=2361678}
@{Name=iexplore; MainWindowHandle=0}
IE with correct HWND
#Nothing?
After this comes the RETURN:
#Also Nothing!

完整代码:

function Start-IELockedDown {
    <#
    .SYNOPSIS
        Open IE without navigation controlls.
    .DESCRIPTION
        Long description
    .EXAMPLE
        PS C:\> Invoke-IELockedDown -URL "http://localhost:8080/"
        Opens a Internet Explorer browser window without navigational controlls that navigates to the specified URL.
    .INPUTS
        None
    .OUTPUTS
        InternetExplorer Process Object.
    .NOTES
        General notes
    #>
    [CmdletBinding()]
    param (
        [string] $URL
    )

    # Create IE com object
    $ie = New-Object -com InternetExplorer.Application

    # Turns off the unnecessary menus and tools and sets the window as resizable
    $ie.AddressBar = $false
    $ie.MenuBar = $false
    $ie.ToolBar = $false
    $ie.Resizable = $true
    $ie.StatusBar = $false

    # Sets the size of the window and make it visible
    $ie.Top = 20
    $ie.Left = 20
    $ie.Width = 1280
    $ie.Height = 1024
    $ie.Visible = $true

    ## Navigate the browser to the specified URL.
    $ie.Navigate($URL)        

    # For Debugging / the Whosebug Guru's
    Write-host "HWND = $($ie.HWND)"
    Write-Host "IE Processes:"
    Get-Process -Name iexplore | Select-Object Name, MainWindowHandle | Write-host
    Write-Host "IE with correct HWND"
    Get-Process -Name iexplore | Select-Object Name, MainWindowHandle | Where-Object {$_.MainWindowHandle -eq $ie.HWND} | Write-Host    
    Write-Host "After this comes the RETURN:"    

    # Return the Process Object for the IE Com Object
    return Get-Process -Name iexplore | Where-Object {$_.MainWindowHandle -eq $ie.HWND}
}

此问题似乎只存在于 PowerShell 的非提升会话中。如果您想解决此问题,您可以 运行 PowerShell 的提升会话(即 运行 作为管理员),或者您可以在导航到 URL 之前捕获 HWND,并引用它.

function Start-IELockedDown {
    <#
    .SYNOPSIS
        Open IE without navigation controlls.
    .DESCRIPTION
        Long description
    .EXAMPLE
        PS C:\> Invoke-IELockedDown -URL "http://localhost:8080/"
        Opens a Internet Explorer browser window without navigational controlls that navigates to the specified URL.
    .INPUTS
        None
    .OUTPUTS
        InternetExplorer Process Object.
    .NOTES
        General notes
    #>
    [CmdletBinding()]
    param (
        [string] $URL
    )

    # Create IE com object
    $ie = New-Object -com InternetExplorer.Application
    $HWND = $ie.HWND

    # Turns off the unnecessary menus and tools and sets the window as resizable
    $ie.AddressBar = $false
    $ie.MenuBar = $false
    $ie.ToolBar = $false
    $ie.Resizable = $true
    $ie.StatusBar = $false

    # Sets the size of the window and make it visible
    $ie.Top = 20
    $ie.Left = 20
    $ie.Width = 1280
    $ie.Height = 1024
    $ie.Visible = $true

    ## Navigate the browser to the specified URL.
    $ie.Navigate($URL)        

    # For Debugging / the Whosebug Guru's
    Write-host "HWND = $($ie.HWND)"
    Write-Host "IE Processes:"
    Get-Process -Name iexplore | Select-Object Name, MainWindowHandle | Write-host
    Write-Host "IE with correct HWND"
    Get-Process -Name iexplore | Select-Object Name, MainWindowHandle | Where-Object {$_.MainWindowHandle -eq $HWND} | Write-Host    
    Write-Host "After this comes the RETURN:"    

    # Return the Process Object for the IE Com Object
    return Get-Process -Name iexplore | Where-Object {$_.MainWindowHandle -eq $HWND}
}