4

I am trying to test our a web page we built that has a temp ssl cert, the issue is that when I goto the page I get the IE security warning that the ssl cert is invalid, with two links one to close the page, and the other to proceed to the page. I was able to use powershell to open IE and click the link on the ssl warning page, but now I need to populate the username and password input boxes and then click the login button.

$url = "res://ieframe.dll/invalidcert.htm?SSLError=50331648#https://10.2.2.1:8050/showme.do"
$ie = New-Object -comobject InternetExplorer.Application 
$ie.visible = $true 
$ie.silent = $true 
$ie.Navigate( $url )
while( $ie.busy){Start-Sleep 1} 
$secLink = $ie.Document.getElementsByTagName('A') | Where-Object {$_.innerText -   eq 'Continue to this website (not recommended).'} 
$secLink.click() 
$ie.Document.getElementsByType("input") | where { $.Name -eq "j_username" }.value = "user"
$ie.Document.getElementsByName("input") | where { $.Name -eq "j_password" }.value = "password"
$loginBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'LoginButton'} 
$loginBtn.click() 

So right now the page opens but the input fields are not populated or the button clicked, Do I need some kind of loop or while statement?

thanks

digitalslavery
  • 75
  • 1
  • 4
  • 13

3 Answers3

4
Write-Output "- Kill all IE windows"
get-process iexplore | stop-process -Force
Start-Sleep -s 1

function that open IE for a given url and a password retrieved from encrypted file

function OpenIE([string]$url, [string]$p)
{

Open Internet Explorer with a given url

$wshell = New-Object -com WScript.Shell
$wshell.Run("iexplore.exe $url")
Start-Sleep 1

Send user credentials to IE

$wshell.sendkeys("+{TAB}")
$wshell.sendkeys("username")
$wshell.sendkeys("{TAB}")
$wshell.sendkeys($p)
$wshell.sendkeys("{ENTER}")
}

$credentialsfile = "C:\temp\credfile.txt" 

Check if credential file exists

if (Test-Path $credentialsfile)
{  

Retrieve the credentials from an encrypted file

$encp = get-content $credentialsfile | convertto-securestring 
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($encp) 
$p = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr) 
remove-variable encp,ptr 
}else{

Request credentials and save to encrypted file

$creds = Get-Credential –credential DOMAIN\user
$encp = $creds.password 
$encp |ConvertFrom-SecureString |Set-Content $credentialsfile
}

Write-Output "- open IE"
OpenIE "http://www.yoururlhere.com" $p
BenMorel
  • 34,448
  • 50
  • 182
  • 322
3

You need to wait until the page finishes loading when you've clicked on a link.

$url = "res://ieframe.dll/invalidcert.htm?SSLError=50331648#https://10.2.2.1:8050/showme.do"
$ie = New-Object -comobject InternetExplorer.Application 
$ie.visible = $true 
$ie.silent = $true 
$ie.Navigate( $url )
while( $ie.busy){Start-Sleep 1} 
$secLink = $ie.Document.getElementsByTagName('A') | Where-Object {$_.innerText -   eq 'Continue to this website (not recommended).'} 
$secLink.click() 
while( $ie.busy){Start-Sleep 1}
$ie.Document.getElementsByType("input") | where { $.Name -eq "j_username" }.value = "user"
$ie.Document.getElementsByName("input") | where { $.Name -eq "j_password" }.value = "password"
$loginBtn = $ie.Document.getElementsById('input') | Where-Object {$_.Type -eq 'button' -and $_.Value -eq 'LoginButton'} 
$loginBtn.click() 
0

You would probably find it a lot easier to work with something like the (free) Windows Automation module for PowerShell up on codeplex:

WASP is a PowerShell snapin for Windows Automation tasks like selecting windows and controls and sending mouse and keyboard events. We have automation cmdlets like Select-Window, Select-Control, Send-Keys, Send-Click, Get-WindowPosition, Set-WindowPosition, Set-WindowActive, Remove-Window ... etc.

Our goal is to enable you to accomplish most Windows GUI Automation scripting from inside PowerShell, without resorting to specialized (and expensive) scripting tools.

http://wasp.codeplex.com/

x0n
  • 51,312
  • 7
  • 89
  • 111