Solved

Powershell Open Private browsing window

Posted on 2015-01-03
7
561 Views
Last Modified: 2015-01-09
Code below opens an IE window for automation process
if (!(Get-Variable ie -Scope global -ea SilentlyContinue) -or !$ie.Document)
{
  $global:ie = New-Object -comobject InternetExplorer.Application
}
$ie.visible = $true
$ie.silent  = $true

Open in new window



My Question is how would I change code above to open a Private browsing IE window instead?
0
Comment
Question by:Leo Torres
  • 4
  • 2
7 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40528946
Unfortunately not and you can not even get a handle to an existing running IE window using [Runtime.InteropServices.Marshal]::GetActiveObject()

Otherwise you could launch with something like..

Start-Process -FilePath "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -ArgumentList ' -private http://www.experts-exchange.com/'

Open in new window

0
 
LVL 8

Author Comment

by:Leo Torres
ID: 40529922
The thing is that this just opens a IE. I need to have powershell to have control of IE browser window to traverse page.

Here what I am doing with that session. I need a private window though.

if (!(Get-Variable ie -Scope global -ea SilentlyContinue) -or !$ie.Document)
{
  $global:ie = New-Object -comobject InternetExplorer.Application
}
$ie.visible = $true
$ie.silent  = $true

$global:ReportPart = "/interface/folderTree.jsp?rootId=216&expandLevel=1&clearUIPath=true&uiPathLabel=Reports"

function Test-ReportSite ([String] $url, [String] $login, [String] $password)
{
  $ie.Navigate2($url)
  while ($ie.busy)                               { Start-Sleep -m 100 }
  while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
   
  $ie.Document.getElementsByTagName("input")  | ? { $_.Id   -eq 'loginField'    } | % { $_.value = $login    }
  $ie.Document.getElementsByTagName("input")  | ? { $_.Id   -eq 'passwordField' } | % { $_.value = $password }
  $ie.Document.getElementsByTagName("button") | ? { $_.Type -eq 'button'        } | % { $_.Click() }
  while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
  
  $ie.Navigate2($url + $ReportPart)
  while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }

  $link = $ie.Document.getElementsByTagName("a") | ? { $_.InnerText -like '*Overtime Report' }  |  select  -First 1 -expandProperty href
  if(!$link) {          
  	$link = $ie.Document.getElementsByTagName("a") | ? { $_.InnerText -like '*Count' }  |  select  -First 1 -expandProperty href           
  }
  $ie.Navigate2($link)
  while ($ie.Document.readyState -ne 'Complete') { Start-Sleep -m 100 }
   
  $ie.Document.getElementsByTagName("button") | ? { $_.innerText -eq 'Go' } | % { $_.Click() }
  Start-Sleep 5

<#  Error message evaluation ignored ATM   
  $ie.Document.getElementsByTagName("span") | ft -a classname, tagname, innerText #Gets you error
#>

  $RunByTest = $ie.Document.body.tagname -eq 'FRAMESET' -and $ie.Document.body.id -eq 'reportViewerFrame'

  if($RunByTest) {
      $TestValue = 200          
  }
  else {          
     $TestValue = -200
  }
  Write-Host "$URL`: $TestValue" 
  return $TestValue
}
                                          

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40529954
For what reason do you need a private IE? Probably you want to disallow caching of the credentials.
I've seen AutoHotkey code sending a Windows Message to make the IE private, but don't know whether that works, and how to convert to PowerShell (yet).
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 8

Author Comment

by:Leo Torres
ID: 40530020
Has to do with cookie created during a session for some sites that causes issues.

Disallowing caching may work. How does the current command change.
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40530063
Seems not to be easy at all. But first I would try to call $ie.Refresh2(3) to force a refresh, ignoring the cache.
0
 
LVL 8

Author Comment

by:Leo Torres
ID: 40539568
Added it at the top of the function.

function Test-ReportSite ([String] $url, [String] $login, [String] $password)
{
  $ie.Refresh2(3) #Here it is
  $ie.Navigate2($url)

Open in new window

0
 
LVL 8

Author Closing Comment

by:Leo Torres
ID: 40539569
I will go with this for now. Thank you!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question