This blog post is a little different from my others. I spent the Christmas days this year to be a bit creative and managed to control my SONOS Environment via Windows PowerShell.
First things first. What I am writing here is probably not supported by SONOS and is currently a preview which is not complete yet or extensively tested. So everything you do is at your own risk. I didn’t break anything while doing my tests, though.
Before I post the preview script, I want to give some facts about how the script works.
SONOS API Documentation
I actually started by looking for the SONOS API Documentation. The Search was quite short because there is actually none API Documentation.
So, what now?
The magic solution here is called: Wireshark
After starting the tool, you can limit the output to the Destination IP of your SONOS Component, connected to Ethernet. To do that, you can use the following filter: ip.dst_host == IP_ADDRESS
In my case I’m talking to a SONOS Playbar. Now you can trigger a Command the official SONOS Controller App. You should see an HTTP/XML POST request to your Destination IP, that looks like the following:
Here you can see, that the HTTP Request has a SOAP Action. If you expand the “eXtensible Markup Language” node at the end, you can view the Envelope that has the actual instruction. In this case I triggered a simple “Play” Action.
The PowerShell Script
With this knowledge, I started writing a PowerShell Script that sends an identical SOAP Action to the Playbar. To my surprise, that actually worked on the first try.
The Preview Script currently has the following 11 actions:
- Previous Track
- Next Track
- Rewind Track
- Repeat All
- Repeat One
- Repeat Off
- Set Volume
The Demo Interface, which triggers the above actions, is console based and pretty simple:
Before you start the script, make sure you open it in ISE or Notepad and change the IP Address.
The Preview script can be downloaded at TechNet Gallery.
So that’s it for today. If you have any questions / comments / improvements, tag me on twitter or below in the Comment Section.