Q:

Automating IE confirmation prompt with Powershell

I have a nice powershell script that automates a particular website for my daughter. Recently they changed the site and added a nice new feature that would speed up my script by a factor of 10. The problem is the input type they use to activate this pops up a confirmation dialog. The HTML looks like this
<input type=submit name='enter_all' value='Enter all' onClick="return confirm('Enter all?');">

Can I automate answering this dialog in powershell? I want the script to run in the background so I think sending keystrokes is out.

A:

I wouldn't bother trying to find out how to click on the confirmation dialog, it looks quite difficult dealing only with COM.
Instead I would simply submit the form like this. Suppose I have this HTML:
<html>
  <body>
  <form action="http://localhost/xx">
  <input type="submit" onclick="return confirm('really?')" value="ENTER" />
  </form>
  </body>
</html>

Than I can bypass the confirmation dialog with this code:
$ie = New-Object -ComObject internetexplorer.application
$ie.Navigate('http://localhost/x.html')
$ie.Visible = 1 # not needed
# look for the correct form, you probably know which one is it
$ie.document.forms | 
   Select -First 1 | 
   % { $_.submit() }   # simply submit...

A:

I have an even easier solution for this. You can override the native window.confirm function with your own. My implementation below replaces it with a confirm function that will bypass the confirm dialog only once.
PowerShell:
$ie = New-Object -COM InternetExplorer.Application -Property @{
    Navigate = "http://www.example.com/"
    Visible = $true
}
do { Start-Sleep -m 100 } while ( $ie.busy )

# Override window.confirm function with confirmOnce, so that on first call
# it will return true and immediately restore it to native confirm.
$jsCommand = @"
    window.confirm = (function() {
        var nativeConfirm = window.confirm;
        function confirmOnce(message) {
            window.confirm = nativeConfirm;
            return true;
        }
        return confirmOnce;
    })();
"@
$document = $ie.document
$window = $document.parentWindow
$window.execScript($jsCommand, 'javascript') | Out-Null

After you run the script, an IE window will open. Press F12 to go to JavaScript console and type:
confirm('Really?')

It will return true. Try to run confirm again, the original confirm dialog will open.

A:

Answering my own question but I did find a way to do this even if it is a bit of a hack.
The main script uses Internet Explorer via Com much like
$ie = new-object -com "InternetExplorer.Application"

There are lots of examples on how to use the ie com object to navigate, fill in fields, click on buttons, etc, etc.  However the pop up confirmation just causes things to hang up.
To get around this I started another thread using Wasp which is a library for automating windows forms. In this thread I just sit in a loop looking for the confirmation prompt and click on it. (The window pops up many times for in my case). The code for this script looks like this
   Import-Module WASP
while ($true) {
  [System.Threading.Thread]::Sleep(200) 
  $confirmation = Select-Window iexplore 
  if ($confirmation -ne $null) {
    Select-ChildWindow  -Window $confirmation | Select-Control -title "OK" -recurse | Send-Click
  }
}

