Monday, October 19, 2020

Desktop Notifications for New Questions on Sitecore Stack Exchange using PowerShell + BurntToast + Windows Task Scheduler

If you're looking for ways to become more proactive in the Sitecore community, one great way to gain traction and potentially make a real impact is to help answer questions on the Sitecore Stack Exchange. You can give yourself opportunities to contribute to the platform by being one of the first users to read and potentially respond to new questions posted on the platform by setting up an alert that notifies you when a new question has been asked. 

The goal here is to create a Windows `Scheduled Task` that runs on a 5-minute interval that consumes the RSS feed and determines if any new questions had been submitted in the last 5 minutes.  For each new question within range, we'll display a Windows notification that links directly to the post

Prerequisite: Install BurntToast

BurntToast is a "PowerShell Module for displaying Windows 10 and Windows Server 2019 Toast Notifications" - and it's pretty neat! 

To set it up on your machine, run the following command:

Configure the PowerShell Script

Create a new PowerShell script in a location on your PC that won't change regularly. If the script is moved after setting everything up, the task will stop working and you'll need to reconfigure it. 

In this example, we'll call the scipt file `SSE_Notify.ps1` and save it in the root of the `C:/` drive.

Copy the following code into your PowerShell script file and save:

Note that the calling `Invoke-WebRequest` to the RSS feed found right on the Sitecore Stack Exchange home page:

We then parse the XML response and add the entries in an array variable - which is then processed if there are new entries. For each entry, we execute a `New-BurntToastNotification` command with the title and button arguments to link back to the post. Also note the icon image path, which should be modified to point to an image file on your machine. Without changing this, BurntToast will default to a predefined icon.

Set up Task Scheduler

Initial Task Setup

Open up the `Windows Task Scheduler`, find an existing folder in the `Task Scheduler Library` in the left-hand panel (or create a new folder, e.g., `BurntToastNotifications`), and select `Create Basic Task` in the right-hand `Actions` pane to open the wizard.
Set the `Name` field to `SitecoreStackExchangeNotification` and click `Next`:
In the `Trigger` panel, keep the `Daily` option selected and click `Next`:
Under the `Daily` task configuration, you can leave everything as-is.
In the `Action` panel, keep the`Start a program` option selected and click `Next`.
In the `Start a Program` panel, set the `Program/script` to `powershell.exe` and the `Add arguments` field to `-File C:\SSE_Notify.ps1`
In the `Finish` panel, check the `Open the Properties dialog for this task when I click Finish` checkbox, then click `Finish`.

Task Configuration Finalization

In the `General` tab, the following steps should be taken to ensure a PowerShell window from popping up when the task executes:
  1. Set the `Configure` the dropdown to `Windows 10`
  2. Check the `Hidden` checkbox.
  3. Select the `Run whether the user is logged on or not` option (this will prompt you for your password when saving the task)
In the `Triggers` tab, click the `Edit...` button:
Under `Advanced settings` pane, change the `Repeat task every` field to `5 minutes` and the `for a duration of` field to `Indefinitely`:
Finally, under the `Conditions` tab, uncheck the `Start the task only if the computer is on AC power` and click `OK`:


When the task runs and a new question had been asked in the last 5 minutes, you'll get a Windows notification on your Desktop:
Notifications will also stack in the Action Center panel after they've been manually or automatically dismissed:
Happy contributing!


Post a Comment