获取由PowerShell创建的现有Internet Explorer窗口

获取由PowerShell创建的现有Internet Explorer窗口,powershell,internet-explorer,Powershell,Internet Explorer,我需要与以前在PowerShell中创建的IE窗口交互,而不使用全局变量 If(WindowAlreadyCreatedByPowerShell){ $IE = WindowAlreadyCreatedByPowerShell }Else{ $IE = New-Object -com internetexplorer.application; $IE.visible = $true; $IE.navigate($url); } 注意:使用((New Obje

我需要与以前在PowerShell中创建的IE窗口交互,而不使用全局变量

If(WindowAlreadyCreatedByPowerShell){
    $IE = WindowAlreadyCreatedByPowerShell
}Else{
    $IE = New-Object -com internetexplorer.application; 
    $IE.visible = $true; 
    $IE.navigate($url);
}

注意:使用
((New Object-coobject Shell.Application.Windows).Invoke()|?{$$\u.Name-eq“Internet Explorer”}
不返回任何内容

,因此您需要在父作用域中存储某些内容,如果您不想存储IE对象,请存储您打开的URL,或者更好,存储窗口句柄(
HWND
属性,该属性应该是唯一的)。否则,您将无法可靠地获取所需的窗口,尤其是当您的会话同时打开其他IE窗口时

# Variable for the created IE window handle
$expectedHWND = $null

# This will enumerate all ShellWindows opened as a COM object in your session
$allShellWindows = ( New-Object -ComObject Shell.Application ).Windows()

# Get the IE object matching the HWND you stored off when you first opened IE
# If you opened other windows in the session, the HWND should be unique so you
# can get the correct window you are expecting
$existingIE = $allShellWindows | Where-Object {
  $_.HWND -eq $expectedHWND
}

# Your code, slightly modified
if( $existingIE ){
  # You should be able to operate on $existingIE here instead of reassigning it to $IE
  Write-Output "Found existing IE session: HWND - $($existingIE.HWND), URL - $($existingIE.LocationURL)"
} else {
  $IE = New-Object -com internetexplorer.application; 
  $IE.visible = $true; 
  $IE.navigate($url);
  $expectedHWND = $IE.HWND
}
如果您运行两次,但第二次不要将
$expectedHWND
设置为
$null
,它将找到您的IE窗口。请注意,要避免在父作用域中存储变量,这需要做大量工作,但在技术上是可行的


您可能需要为您的特定应用程序策划上述示例,但这是为您之前打开的IE窗口获取可操作对象的一个很好的演示。

因此,您需要在父范围中存储一些内容——如果您不想存储IE对象,请存储您打开的URL,甚至更好呃,窗口句柄(
HWND
属性,应该是唯一的)。否则,您将无法可靠地获取所需的窗口,尤其是当您的会话同时打开其他IE窗口时

# Variable for the created IE window handle
$expectedHWND = $null

# This will enumerate all ShellWindows opened as a COM object in your session
$allShellWindows = ( New-Object -ComObject Shell.Application ).Windows()

# Get the IE object matching the HWND you stored off when you first opened IE
# If you opened other windows in the session, the HWND should be unique so you
# can get the correct window you are expecting
$existingIE = $allShellWindows | Where-Object {
  $_.HWND -eq $expectedHWND
}

# Your code, slightly modified
if( $existingIE ){
  # You should be able to operate on $existingIE here instead of reassigning it to $IE
  Write-Output "Found existing IE session: HWND - $($existingIE.HWND), URL - $($existingIE.LocationURL)"
} else {
  $IE = New-Object -com internetexplorer.application; 
  $IE.visible = $true; 
  $IE.navigate($url);
  $expectedHWND = $IE.HWND
}
如果您运行两次,但第二次不要将
$expectedHWND
设置为
$null
,它将找到您的IE窗口。请注意,要避免在父作用域中存储变量,这需要做大量工作,但在技术上是可行的


您可能需要为您的特定应用管理上述示例,但这是一个很好的示例,可以为您以前打开的IE窗口获取一个可操作的对象。

您的意思是,您希望获取创建的窗口,而不在父范围中指定var?这是正确的。我会看看我能想出什么,但ea最简单的方法是将
$IE
放在父作用域中。我知道您可以从
$IE
ComObject中获取
HWND
,但要获取基于
HWND
的对象,您可能需要将
P/Invoke
插入win32 API,并且我不确定是否可能从现有
HWND获取
$IE
ComObject
handle。我也很好奇为什么要避免在父作用域中声明
$IE
。找到了一个不需要
P/Invoke
的解决方案,并且仍然使用窗口句柄来找到正确的窗口。但是,为了避免在父作用域中使用变量,这仍然需要做大量的工作,因为您实际上是在抛出取消对象分配只是为了弄清楚它以后是哪个对象。你是说你想获得创建的窗口而不在父范围中指定var?这是正确的。我会看看我能想出什么,但最简单的方法是将
$IE
放在父范围中。我知道你可以从获取
HWND
ode>$IEComObject但要基于
HWND
获取对象,您可能需要
P/Invoke
到win32 API中,我不确定是否可以从现有
HWND
句柄获取
$IE
ComObject。我还很好奇为什么您要避免在父范围中声明
$IE
。找到一个解决方案不需要
P/Invoke
,并且仍然使用窗口句柄来查找正确的窗口的操作。但是为了避免在父范围中使用变量,这仍然是一项大量的工作,因为您实际上放弃了对象赋值,只是为了弄清楚它以后是哪个对象。