PowerShell Scripts - OStillman/HomeAssistantTeamsStatus GitHub Wiki

Setting up Powershell scripts

Step 1 - Grab the Files

If you haven't already, clone this Repo onto a local device - your Raspberry Pi you will use for Blinkt! output will work nicely!

Copy the files from PowerShellScripts/ to your Windows PC - save them in an easily accessible area. Mine are located on my Desktop

Step 2 - Edit MainScript.ps1

MainScript.ps1 contains a file location used when a Teams status change is detected. This currently points to the location I use for these files and should be changed for your location.

Look for the "C:\Users\...\TeamsStatus.ps1" part of the file.

Edit this as below:

  • Reference your area you have stored the file
  • Be sure to preserve the speachmarks at the start and end of the directory location

Step 3 - Edit All Other Scripts

Once complete, the scripts used for direct Home-Assistant communication need to be edited. (I am aware these aren't the most efficient, but they work for the quick and dirty approach in use here)

Begin by creating a new Bearer token in Home Assistant:

  • Log in as your desired user (I use a "bot" user that is seperate from all other users to keep the actions log-book friendly, but you can use any account you wish)
  • Click the account name on the sidebar
  • Scroll to the bottom
  • Click Create Token
  • Once you have been shown the token, store it somewhere secure you WILL NOT be shown this again

Edit the files to carry out two actions:

  1. Change URL/IP address to point towards your Home Assistant
  2. Replace [your_token_here] with your token from Home Assitant e.g. if my token was 1a3b4c5b (yours will be MUCH longer) I would enter it to be left with: "Bearer 1a3b4c5b". Again, be sure to preserve the speechmarks!

Step 4 - Create the Scheduled Tasks

For each file, we now need to make sure Windows knows when to run them so for each status change we can report back to Home Assistant.

It's important these are done as below:

  • MainScript.ps1 - We need this to run At Log on
  • LockSend.ps1 - To run when the user locks their PC
  • Unlocked.ps1 - To run when the user unlocks their PC
  • TeamsStatus.ps1 - No need to run this on the task schedular, this is invoked by MainScript.ps1
  • Shutdown.ps1 - Currently doesn't work, I've not figured out a way to run this on Shutdown of the PC.

So Let's create a scheduled task for each Script:

To use Task Scheduler (we need one per script):

  • Create a new Folder, right click "Task Scheduler Library", click New Folder and Name it e.g. "My Tasks" - This keeps these seperate from all other Windows Tasks
  • Click Create Task...
  • Enter a name
  • Click the Triggers tab
  • Click New...
  • Select the Begin the Task: dropdown as required. These are pretty self-explanatory for each script e.g. MainScript.ps1 needs to run when the user logs in, so select "At Log on"
  • Click Ok
  • Click Actions
  • Click New...
  • In Program/Script type powershell
  • In Add arguments (optional) enter the following -NoProfile -ExecutionPolicy Bypass -nologo -windowstyle Hidden -File "C:\Users\...\TeamsScripts\MainScript.ps1"
    • Substitute the file location to match your full file location
    • Substitute MainScript.ps1 for the correct script name as required
  • Click Ok + Repeat for each other script