Hi
DISCLAIMER: The paid version of PowerShell Universal is very expensive, and the free version does not support any kind of authentication what.so.ever. Use at own risk. Mitigate with firewalling rules.
Now that I got that out of the way:
PowerShell Universal (link) is a PowerShell web API that allows you to run PowerShell scripts on target machine(s) with simple GET/POST’s. I use it to do stuff on my Hyper-V server and on my Kodi machine. I have few scripts setup to start and stop VM’s from HA, and for Kodi I can restart some processes that are prone to crashing (very old case with an LCD built-in, with even older software). But the possibilities are limitless - you could even parse eventlogs, start/stop services, programs, check Windows Update status, uptime, disk usage, anything you like/need and can code in PowerShell (I know there are alternatives to this, I just like PowerShell).
To give you guys an example:
I have the following switches in my config:
- platform: command_line
switches:
hue_sync:
command_on: /usr/bin/curl -sS -X GET http://kodi.my.internal_fqdn:5000/start_hue_sync
command_off: /usr/bin/curl -sS -X GET http://kodi.my.internal_fqdn:5000/stop_hue_sync
command_state: /usr/bin/curl -sS -X GET --max-time 60 http://hypervisor.my.internal_fqdn:5000/get_hue_sync
value_template: '{{ value == "[0]" }}'
friendly_name: Light Sync
mount:
command_on: /usr/bin/curl -sS -X GET http://kodi.my.internal_fqdn:5000/start_mounts
command_off: /usr/bin/curl -sS -X GET http://kodi.my.internal_fqdn:5000/stop_mounts
command_state: /usr/bin/curl -sS -X GET --max-time 60 http://hypervisor.my.internal_fqdn:5000/get_mounts
value_template: '{{ value == "[0]" }}'
friendly_name: Disk Mount
restart_display:
command_on: /usr/bin/curl -sS -X GET http://kodi.my.internal_fqdn:5000/restart_display
command_off: /usr/bin/curl -sS -X GET http://kodi.my.internal_fqdn:5000/restart_display
command_state: /usr/bin/curl -sS -X GET --max-time 60 http://hypervisor.my.internal_fqdn:5000/get_display
value_template: '{{ value == "[0]" }}'
friendly_name: Display Process
restart_kodiexe:
command_on: /usr/bin/curl -sS -X GET http://kodi.my.internal_fqdn:5000/start_kodi
command_off: /usr/bin/curl -sS -X GET http://kodi.my.internal_fqdn:5000/stop_kodi
command_state: /usr/bin/curl -sS -X GET --max-time 60 http://hypervisor.my.internal_fqdn:5000/get_kodi
value_template: '{{ value == "[0]" }}'
friendly_name: Kodi Process
The four switches:
Hue Sync: will launch Hue Sync application on Kodi to start syncing the Hue Play lights to video.
Mount: will mount an encrypted VeraCrypt volume on Kodi which contains, euhh, private stuff.
Restart display: will kill and restart application process that takes care of an old LCD display on my case
Restart kodi.exe: will kill and restart Kodi process.
As you can see, it’s just some GET’s to my Kodi and Hypervisor machines. I use my hypervisor for state because if Kodi is powered down, the state check timeout will appear in the HA logs. I check Hypervisor, Hypervisor tries the check state on Kodi, and returns 'Off" value to HA if Kodi times out. That way HA always has an answer and nothing fills the log files.
Now to the interesting part. On Kodi, in powershell universal, I have several endpoints set up:
Those endpoints contain the commands I want Kodi to run when a GET request arrives. For example, lets have a look how I start Hue Sync application:
I have a Scheduled Task without a trigger that runs with my credentials, so that when the Scheduled Task starts, it starts the huesync.exe with my user, and spawns the application in my user session.
I use the endpoint as a trigger, and start the Task in PowerShell:
if(((Get-Process HueSync -ErrorAction Ignore).id -eq $null) -and ((Get-ScheduledTask HueSync).state -eq "Ready"))
{
Start-ScheduledTask -TaskName HueSync
}
Basically, if huesync.exe does not exist and status of the Scheduled Task is not ‘Running’, start the task.
To stop HueSync I just:
Get-Process HueSync | Stop-Process -Force
To get the state of HueSync, I have the following:
Which either returns 0 or 2 to the Hypervisor, which in turn returns same value to HA. This is the endpoint on Hypervisor for Hue state on Kodi:
As you can see, this is pretty simple to setup. Only requiremet is a little knowledge of powershell, and you’re off.
I hope someone can do cool stuff with this. Please at least firewall your PowerShell Universal API port and only allow traffic from HA to work around the lack of authentication. Have fun, and let me know if this helped in any way.
Grtz
Lukas