How to close a tab in existing IE window using powershell

霸气de小男生 提交于 2021-01-28 12:41:07

问题


Hi Does anyone know how to open and close a tab in internet explorer using powershell. I am able to open one using the following

 $navOpenInNewTab = 0x800
 # Get running Internet Explorer instances
 $oApp = New-Object -ComObject shell.application
 # Grab the last opened tab
 $oIE = $oApp.Windows() | Select-Object -Last 1
 # Open link in the new tab nearby
 $oIE_Total_Sends = $oIE.navigate($link, $navOpenInNewTab) 
 while ($oIE.busy) {
 sleep -milliseconds 50
 }

How do i close this new tab through powershell and why can't I access Internet Explorer methods like getElementByID on $oIE.Document object.


回答1:


Here is a function that will close a tab in IE.

Function Close-IETab {
param($url)

    $oWindows = (New-Object -ComObject Shell.Application).Windows

    foreach ($oWindow in $oWindows.Invoke()) {

        if ($oWindow.Fullname -match "IEXPLORE.EXE" -and $oWindow.LocationURL -match $url) {

            Write-verbose "Closing tab $($oWindow.LocationURL)"
            $oWindow.Quit()
        }
    }

}


来源:https://stackoverflow.com/questions/49575365/how-to-close-a-tab-in-existing-ie-window-using-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!