0

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.

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.

# 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

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.

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -File C:\Users\MyName\Documents\Powershell\login_IE.ps1

However, if I create a scheduled task in windows task scheduler executing this script, it doesn't work. I fill the "Program/script:" as:

C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe

and fill the "Add arguments (optional):" as:

-ExecutionPolicy RemoteSigned -File C:\Users\MyName\Documents\Powershell\login_IE.ps1

The scheduled task is run under my account (I am the only user of this computer).

If I run the scheduled task manually, 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. But I found I am not logged in since I don't have internet access, where could the problem be?

Any advice is really appreciated. Thanks in advance.

4
  • In Task Scheduler, have you checked Run whether user is logged on or not radio button and Run with highest privileges checkbox. I think those are required. Commented Jan 8, 2018 at 7:04
  • @VivekKumar Yes I have both checked. Commented Jan 8, 2018 at 11:21
  • Then I would suggest you to check for the logs by using the Start-Transcript and Stop-Transcript cmdlets. It outputs all (would be) screen content to a log file. Commented Jan 8, 2018 at 11:30
  • @VivekKumar I tried that, run the script in task scheduler, and there is no error in the log file, but still couldn't get logged in. Commented Jan 9, 2018 at 1:27

1 Answer 1

0

Similar type of issue I had, when trying to run the script directly from Powershell window it works as expected, but from the task scheduler or command line both not getting the desired results.

Commenting and adding lines like below in my script, help me to run the script from command line and task scheduler as well

$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols

[Net.ServicePointManager]::SecurityProtocol = 
[Net.SecurityProtocolType]::Tls12

Hope this helps anyone else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.