No API, no problem. Logging into websites with PowerShell

24 Apr

In the name of automating everything, a recent opportunity arose to make life a bit easier when it came to organising a regular sporting event. However, the 2 websites that we use don’t have any API support whatsoever.  A little annoying, but I was fairly confident that I could find a workaround anyway.

The plan was some like this:

A master task that would run every Tuesday morning (we play on a Monday evening).Find out if the day of the game Monday next week is a bank holiday, if it is, post a Teamer message saying this and leave it there until the next week. If not, login to and create an event for the next week. Then send the notifications to all players. Then create a secondary task to check teamer every hour to see how many people have confirmed that they can play.  My magic number variable was set at 10. So, If the number of people confirmed LT 10, exit. However also check that if the current date time GT 14:00 on the day of the game then cancel the game on teamer and add the reason of “Not enough players”. Also, cancel this secondary scheduled task. If the number of confirmed players GE 10, then login to the website for the sports hall and check that it is available for the time we want. If it isn’t, cancel the game on teamer and give the reason of “Hall unavailable”. If it is available then add a message on Teamer to book the hall ASAP. In both these cases, cancel the second scheduled task. Pretty simple.

I won’t paste all the code, but here are some snippets:

Logging in to the website

$url = ""
$username = "" 
$password = "mypassy" 
$magicnumber = 10

#login to teamer 
$ie = New-Object -com InternetExplorer.Application 
start-sleep 2
while($ie.ReadyState -ne 4) {start-sleep -m 100} 
$ie.document.getElementById("email").value= $username 
$ie.document.getElementById("password").value = $password 
start-sleep 5 

Find out if next week is a bank holiday and set date time variables

$result = Invoke-WebRequest "" -Method Get | convertfrom-json
$nextmonday = (Get-Date).AddDays(6).ToString("yyyy-MM-dd")
$nextmonday_teamerformat = (Get-Date).AddDays(6).ToString("dd/MM/yyyy 20:50")

$gameonbh = $false
foreach($event in $result."england-and-wales".events)
    if($ -eq $nextmonday){
        $gameonbh = $true

Click Add Event and Populate

#open add event window
    while($ie.ReadyState -ne 4) {start-sleep -m 100} 
    start-sleep 5 

    #enter details
    $ie.document.getElementByID("ane_the_event_type").value = "Training" 
    $ie.document.getElementByID("datepicker_event_date").value = $nextmonday_teamerformat
    $ie.document.getElementByID("new_event_venue").value = "Sports Hall, UK"
    $ie.document.getElementByID("new_keyword").value = "Place, Postcode, UK"
    $ie.document.getElementByID("new_event_non_game_description").value = "£5 per player" 

    #sumbit event
    $Click=$ie.Document.getElementsByTagname("button") | Where-Object {$_.type -eq "submit" -and $_.ClassName -eq "btn btn-primary btn-m pull-right"} 

Kill IE

   $shellapp = New-Object -ComObject "Shell.Application"
    $ShellWindows = $shellapp.Windows()
    for ($i = 0; $i -lt $ShellWindows.Count; $i++)
    if ($ShellWindows.Item($i).FullName -like "*iexplore.exe")
    $ie = $ShellWindows.Item($i)

If anyone wants a copy of the full code, just drop me a line.