Texting Yourself Sports Alerts with PowerShell

Fri, Feb 21, 2014 One-minute read

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.)

$content = ""
    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

    } -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 = "[email protected]"
        From = "[email protected]"
        Subject = "Hockey Score Update"
        Body = $SCRIPT:content
        SmtpServer = "mysmtpserver"

    Send-MailMessage @params