title: Running powershell script to perform automated webpage login from task schedular behaves differently from running the script manually
body: <p>My university requires all computers to perform a web-based login in order to get-access to the internet, and claims that all users will log-off automatically in the mid-night (sounds strange, but it is true), so I am trying to write a powershell script (in Windows 10) to perform automatic login at mid-night.</p>
  
  <p>My script is list here. It opens an IE process in the background (in a nonvisible way), fill in the username and password, login, and kills the IE process.</p>
  
  <pre><code># If there are existing Internet Explorer processes, close it
  $IE_Process = Get-Process iexplore -ErrorAction Ignore
  if ($IE_Process) { 
      $IE_Close = Foreach-Object { $IE_Process.CloseMainWindow() } 
  }
  
  Stop-Process -Name "iexplore" -ErrorAction Ignore
  
  # Login Information
  $url = "http://xxx.xxx.xxx.xxx/"
  $username = "xxxxxxxx" 
  $password = "xxxxxxxx" 
  
  # Open an IE process
  $ie = New-Object -com internetexplorer.application; 
  $ie.silent = $true 
  $ie.navigate($url); 
  while ($ie.Busy -eq $true) 
  { 
      Start-Sleep -s 1; 
  } 
  
  # The stupid webpage needs to submit twice
  $ie.Document.getElementById("loginname").value = $username 
  $ie.Document.getElementByID("password").value = $password 
  $ie.Document.getElementById("button").Click()
  Start-Sleep -s 1; 
  $ie.Document.getElementById("loginname").value = $username 
  $ie.Document.getElementByID("password").value = $password 
  $ie.Document.getElementById("button").Click()
  
  # Close the IE process
  $IE_Process = Get-Process iexplore -ErrorAction Ignore
  if ($IE_Process) { 
      $IE_Close = Foreach-Object { $IE_Process.CloseMainWindow() } 
  }
  
  Stop-Process -Name "iexplore" -ErrorAction Ignore
  
  Remove-Variable -Name ie,username,password,url,IE_Process -ErrorAction Ignore
  </code></pre>
  
  <p>The script is saved as "login_IE.ps1". It may be poorly written as I am new to powershell, but it works. If I open an cmd window and execute the following command, I am logged in.</p>
  
  <pre><code>C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -File C:\Users\MyName\Documents\Powershell\login_IE.ps1
  </code></pre>
  
  <p>However, if I create a scheduled task in windows task scheduler executing this script, it doesn't work. I fill the "Program/script:" as:</p>
  
  <pre><code>C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
  </code></pre>
  
  <p>and fill the "Add arguments (optional):" as:</p>
  
  <pre><code>-ExecutionPolicy RemoteSigned -File C:\Users\MyName\Documents\Powershell\login_IE.ps1
  </code></pre>
  
  <p>The scheduled task is run under my account (I am the only user of this computer).</p>
  
  <p>If I run the scheduled task manually, <strong>in the task manager I can see two IE process opened in the "Background process", communicate with the internet, and then get killed, so I am pretty sure that the script has actually been executed.</strong> But I found I am not logged in since I don't have internet access, where could the problem be?</p>
  
  <p>Any advice is really appreciated. Thanks in advance.</p>