title: Powershell scheduled job; Internet Explorer not visible
body: <p>I'm trying to create a PS logon script that will listen to a serial port, pass on the data to its original application, parse the data, and use the result to open an internal server and display an information page relating to the data.</p>
  
  <p>I've gotten it working from an active powershell session, but when I run it as a scheduled task I can't see the IE that it calls.  I know that it is actually loading and performing login and lookup because I can see a new user session created on our internal webpage, and each time I feed the serial port a new barcode scan I can see that it passes it on towards the originally intended application.</p>
  
  <p>I've seen some discussion on TechNet that seems to indicate that using IE in this way isn't possible, but then how is it managing to login to the site?</p>
  
  <pre><code># Variables
  $username = ""
  $password = ""
  $meterno = ""
  $rawmeterno = ""
  $ReceivePort = "COM6"
  $SendPort = "COM7"
  
  
  Function Initialize () {
  $global:RCport = $null
  $global:SNport = $null
  
  $global:RCport = new-object system.io.ports.serialport $ReceivePort,9600,none,8,one
  $global:RCport | add-member -membertype "NoteProperty" -name "identifier" -value "ReceiveSerial"
  $global:RCport.open()
  #$global:RCport
  $global:SNport = new-object system.io.ports.serialport $SendPort,9600,none,8,one
  $global:SNport | add-member -membertype "NoteProperty" -name "identifier" -value "SendSerial"
  $global:SNport.open()
  #$global:SNport
  }
  
  Function ParseMeter($rawmtr){
  $meter = $rawmtr |
   %{
    if ($_ | sls -pattern "SD") {
     ([regex]"[0-9]{5}SleepDrinks").matches($_) | % {$_.value}}
    elseif ($_ | sls -pattern "TD/TR") {
     $TD = ([regex]"[T]Drinks[/][T][R][0-9]{3}").matches($_) | %{$_.value}
     $TD = ($TD -replace "/TR", "")
     $TD}
    else {$_ | % { $_.tostring().split()[1] }}
    }
  write-host "ParseMeter result: $meter `n"
  $meter
  }
  
  Function Main {
   Do{ 
    $rawmeterno = $global:RCport.readline()
    write-host $rawmeterno
    $meterno = $rawmeterno | %{ParseMeter($_)}
    $global:SNport.writeline("$rawmeterno")
  
    #check for ie
    $ie
    if (!($ie)) {
     $ie = New-Object -com InternetExplorer.Application
     $ie.visible=$true
     sleep -s 2}#if
    $ie.navigate2("http://server/endpointlookup.aspx")
    sleep -s 2
    $ie.locationname
    $ie
  
    #login
    if ($ie.locationname -eq "http://server/login.aspx?ReturnUrl=%2fcc%2fendpointlookup.aspx") {
     sleep -s 2
     write-host "Login Function"
     $ie.visible = $true
     $ie.navigate2("http://server/login.aspx")
     while($ie.busy) {start-sleep -s 2}
     $ie.document.getElementById("LoginName").value= $username
     sleep -s 1
     $ie.document.getElementById("Password").value = $password
     sleep -s 1
     $ie.document.getElementById("LoginBtn").click()
     sleep -s 3
     while($ie.busy) {start-sleep -s 2}
  
     #lookup
     $ie.navigate2("http://server/endpointLookup.aspx")
     while($ie.ReadyState -ne 4) {start-sleep -s 1}
     sleep -s 1
     $meterno
     $ie.document.getElementById("ctl00_PageBody_MeterNumberBox").value = $meterno
     write-host "meterno assigned"
     sleep -s 2
     $ie.document.getElementById("ctl00_PageBody_RetrieveButton").click()
     write-host "button clicked"
     sleep -s 2
     write-host "about to assign 'details' to variable"
     $details = @( $ie.document.getelementsbytagname('a')) | where-object {$_.innerText -eq 'Details'}
     $details
     write-host "variable assigned, about to click"
     $details.click()
     write-host "details clicked"
     Trap{"$_" ; $global:SNport.close(); $global:SNport.dispose(); $global:RCport.close(); $global:RCport.dispose(); $ie.quit() ;sleep -s 2; Break }
    }#if
  
    #lookup
    else { 
     $ie.navigate2("http://server/endpointLookup.aspx")
     while($ie.ReadyState -ne 4) {start-sleep -s 1}
     write-host "Lookup Function"
     $ie.visible = $true
     $ie.document.getElementById("ctl00_PageBody_MeterNumberBox").value = $meterno
     write-host "Meterno assigned"
     sleep -s 1
     $ie.document.getElementById("ctl00_PageBody_RetrieveButton").click()
     write-host "clicked GO"
     sleep -s 1
     $elsedetails = @( $ie.document.getelementsbytagname('a')) | where-object {$_.innerText -eq 'Details'}
     write-host "variable assigned, about to click"
     $elsedetails.click()
     write-host "details clicked"
     Trap{"$_" ; $global:SNport.close(); $global:SNport.dispose(); $global:RCport.close(); $global:RCport.dispose(); $ie.quit() ;sleep -s 2; Break }
     }#else
   }#do
   while ($global:RCport.isopen -eq $true)
  }#function - Main
  
  Initialize
  sleep -s 2
  Main
  </code></pre>
  
  <p>The scheduled task is set to run at login, elevated.  And I'm testing this on Win7(64) using IE10 and PS 4.0.</p>
  
  <p>As you can see in the script, I set IE to visible in several places.  Any ideas?</p>
  
  <p>Thanks for any feedback!</p>