Windows Powershell scripting to control your Home Assistant (and other) virtual machines!

This project is in an alpha state, so alpha that for now, the only instructions will be provided in this topic.

I don’t like Raspberry Pi.

Ok, that’s not true at all. It’s a great piece of technology to run linux stuff on the cheap. I should say I don’t like Raspberry Pi for running Home Assistant.

I love Virtual Machines

That’s not true either. The are really handy however, be it running in the cloud or on your local infrastructure it’s a great way to fire up new machines for dedicated purposes. But what I do love is the ability to take and restore machine states, or as they call them in Oracle’s VirtualBox snapshots.

When I first started playing with Home Assistant, I did use a Pi. But I found myself constantly formatting SD cards, copying files, reconfiguring, etc. It was a pain in the neck. So I took my Intel NUC, spent a hundred bucks or so on memory, installed a Ubuntu Virtual Machine and configured Home Assistant on that. And it works great.

However, there are certain problems with VirtualBox that make it difficult to host a live service machine. And those are when my Host machine (my Intel NUC) boots and reboots. I have to manually login to the Host and start/stop/restart my VM running Home Assistant. So during the COVID lockdown I spent some time writing some Windows Powershell scripts to handle some common tasks. With these scripts you can do the following:

These scripts work for Windows 10. (Note, these scripts require you to use an Auto login feature to automatically login when the system boots)

  • Auto-Start one or more Virtual Machines in headless mode upon boot. These VM’s can also be controlled via the VirtualBox GUI. To add/remove machines to this procedure is as simple as adding/removing the machine names to a text file.
  • Shutdown all Virtual Machines.
  • Take a “clean” Snapshot of a virtual machine. This will shutdown the VM, take a Snapshot, and restart the VM.
  • IFTTT support!
    • Hey Google, take a Snapshot will shutdown your Home Assistant VM, take a Snapshot, and restart the VM. I use this all the time to save a working state of my Home Assistant instance before I experiment with a new feature, be it large or small. No committing and pushing to Github, no exporting config files, etc. It’s saved my bacon many times already
      Hey Google, reboot NUC will shutdown all running VM’s and reboot my Intel NUC, which will then restart my list of Virtual Machines. This is handy if you automatically install Windows Updates.

Auto-Start
To Auto-Start machines, clone the repo to a directory of your choice. Copy a shortcut to StartupVMs.ps1 and paste that in your startup folder (Press Windows-R and paste shell:startup and press enter to open this location in Explorer). Edit StartupVMs.ini and put the names of every VM you want to Auto-Start (one per line)

IFTTT

In IFTTT you create a new service, where you use your voice assistant in conjunction with OneDrive or Dropbox to create a text file in your account which will be processed by your powershell script.

With your voice assistant, create a new service:

  • the IF is Say a phrase with a text ingredient
  • the What do you want to say is execute $ (then click Create Trigger)
  • the "that* use OneDrive service to “Create a text file”. Use the filename and location that you specify in IFTTT_Monitor
  • For the file content, specify:
{{TextField}}<br>
<br>

Now test the applet. Say “Ok Google, Execute testing” and then you should see a new file created on your machine in the location you specified, whose contents will read testing.

Now you need to create a new Task Scheduler event that will start the process to monitor commands from IFTTT. Create a new Task and name it IFTTT Monitor

  • Trigger: Run at User logon (enable autologon)
  • Action: Start a program: powershell.exe Arugments: -windowstyle hidden -f “IFTTT_Monitor.ps1”
  • Modify IFTTT_Monitor.ps1 and specify the path and name of the cloudbased file which will contain the command created by IFTTT. e.g. D:\OneDrive\ifttt\cmd.txt
  • Modify IFTTT_Monitor.ps1 to choose what PS scripts to launch based on keyword

Make sure IFTTT_Monitor.ini contains the pathname of the file created by IFTTT and synched by OneDrive.

Modify the switch statement in IFTTT_Monitor.ps1 to add the keyword passed into cmd.txt. Specify the Powershell script to run including (if any) arguments to use.

3 Likes

Anyone try this out?

No, but I use https://iotlink.gitlab.io/ to run commands on a PC, as well as get temp/cpu/ram/storage data over matt. You can also send shell commands to snapshot a vm.

Does this run as a windows service?

You can create a Scheduled Task that runs at startup.

Scheduled task: New Task
Trigger: New, At startup
Actions: New, Start a program
Program/scripts:
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe"

Add Arguments:
startvm HASSioVMName --type headless

Save

Borrowed from here