Internet explorer 损坏的IE对象(带有Powershell的IE自动化)

Internet explorer 损坏的IE对象(带有Powershell的IE自动化),internet-explorer,powershell,automation,com-object,Internet Explorer,Powershell,Automation,Com Object,我想使用Powershell在web控制台上自动配置accesspoint 我是在Win8/IE10上开发的,在测试代码时一切正常。 不幸的是,我在开发/测试之后收到了“生产系统”。 现在,该代码应该可以在Windows Server 2012 R2/IE11上运行 这是我连接和登录accesspoint的代码(简化): 出于调试原因,我通常会显示窗口(注释1)。 在第一次写入主机时,会显示正确的位置(“登录”)。 在第二个写入主机上,显示错误的位置(“关于:空白”)。 在IE窗口中,您可以看到

我想使用Powershell在web控制台上自动配置accesspoint

我是在Win8/IE10上开发的,在测试代码时一切正常。 不幸的是,我在开发/测试之后收到了“生产系统”。 现在,该代码应该可以在Windows Server 2012 R2/IE11上运行

这是我连接和登录accesspoint的代码(简化):

出于调试原因,我通常会显示窗口(注释1)。 在第一次写入主机时,会显示正确的位置(“登录”)。 在第二个写入主机上,显示错误的位置(“关于:空白”)。 在IE窗口中,您可以看到正确的位置/选项卡名(“AP”)。 IE对象似乎变得不可用,其他属性(如$IE.Document.*)则不起作用。我得手动关窗

我有两个变通办法:

  • 当我在登录前手动单击IE窗口(获取焦点)时,它 工作
  • 当我不显示窗口($IE.Visible=$false)时 一切正常
  • 到目前为止,我的场景/测试:

  • 电子稳定控制系统已禁用
  • 我从另一台生产机器(2008R2)复制了C:\Program Files(x86)\Microsoft.NET\Primary Interop Assemblys\Microsoft.mshtml.dll,因此$IE.Document函数可以工作(来自我的开发机器的dll会产生相同的错误)
  • 我尝试为每个区域启用/禁用保护模式(Internet选项>安全),但对我无效(在上找到)
  • 其他人有想法试试吗? 调试工作与变通方法,它也工作(到目前为止)与不可见的窗口-但我想了解的麻烦

    谢谢,致以最良好的祝愿,
    kvo

    我在将脚本部署到运行在IE11上的windows server 2012时遇到了这个问题

    原因是失去了Powershell创建的IE对象的处理程序

    我使用了在中编写的以下代码段来解决此问题

    function ConnectIExplorer() {
        param($HWND)
    
        $objShellApp = New-Object -ComObject Shell.Application 
        try {
          $EA = $ErrorActionPreference; $ErrorActionPreference = 'Stop'
          $objNewIE = $objShellApp.Windows() | ?{$_.HWND -eq $HWND}
          $objNewIE.Visible = $true
        } catch {
          #it may happen, that the Shell.Application does not find the window in a timely-manner, therefore quick-sleep and try again
          Write-Host "Waiting for page to be loaded ..." 
          Start-Sleep -Milliseconds 500
          try {
            $objNewIE = $objShellApp.Windows() | ?{$_.HWND -eq $HWND}
            $objNewIE.Visible = $true
          } catch {
            Write-Host "Could not retreive the -com Object InternetExplorer. Aborting." -ForegroundColor Red
            $objNewIE = $null
          }     
        } finally { 
          $ErrorActionPreference = $EA
          $objShellApp = $null
        }
        return $objNewIE
      } 
    
    
    
    
    $HWND = ($objIE = New-Object -ComObject InternetExplorer.Application).HWND
    $objIE.Navigate("https://www.google.com")
    $objIE = ConnectIExplorer -HWND $HWND
    
    function ConnectIExplorer() {
        param($HWND)
    
        $objShellApp = New-Object -ComObject Shell.Application 
        try {
          $EA = $ErrorActionPreference; $ErrorActionPreference = 'Stop'
          $objNewIE = $objShellApp.Windows() | ?{$_.HWND -eq $HWND}
          $objNewIE.Visible = $true
        } catch {
          #it may happen, that the Shell.Application does not find the window in a timely-manner, therefore quick-sleep and try again
          Write-Host "Waiting for page to be loaded ..." 
          Start-Sleep -Milliseconds 500
          try {
            $objNewIE = $objShellApp.Windows() | ?{$_.HWND -eq $HWND}
            $objNewIE.Visible = $true
          } catch {
            Write-Host "Could not retreive the -com Object InternetExplorer. Aborting." -ForegroundColor Red
            $objNewIE = $null
          }     
        } finally { 
          $ErrorActionPreference = $EA
          $objShellApp = $null
        }
        return $objNewIE
      } 
    
    
    
    
    $HWND = ($objIE = New-Object -ComObject InternetExplorer.Application).HWND
    $objIE.Navigate("https://www.google.com")
    $objIE = ConnectIExplorer -HWND $HWND