Texting Yourself Sports Alerts with PowerShell

You’ve probably been in the situation of wanting to alert yourself when any update happens to a sports game, UPS package tracking status, or something else.

By combining Invoke-WebRequest’s beautiful support for HTML parsing with Send-MailMessage’s ability to send email messages – this becomes incredibly easy and useful.

(Note: this script also uses Watch-Command, a script from the PowerShell Cookbook.)

 

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
$content = ""
while($true
)
{
   
Watch-Command -ScriptBlock
 {

        ## Fetch the current box score for the game
        $r = Invoke-WebRequest http://www.tsn.ca/MENS_WORLD/scores/boxscore/?id=2388586

        ## The score is in a DIV with the class ‘boxScoreBg’, almost certainly different
        ## for your scenario
        $result = $r.ParsedHtml.Body.getElementsByClassName("boxScoreBg"
)

        ## Extract out what text you care about
        $SCRIPT:content = (@($result)[0].innerText -split "`r`n" | ? { $_ })[5..6] -join "`r`n"

        ## And use that to monitor
        $SCRIPT:content

    } -UntilChanged -DelaySeconds 30

    ## Every cell provider has an email address that you can send to
    ## so that it will be delivered as a text message.
    $params =
 @{
        To 
= "4255551212@txt.att.net"
        From = "my.email@host.com"
        Subject = "Hockey Score Update"
        Body = $SCRIPT:content
        SmtpServer = "mysmtpserver"
    }

    Send-MailMessage @params
}

Leave a Reply