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 found on GitHub.
So that’s it for today. If you have any questions / comments / improvements, tag me on twitter or below in the Comment Section.
here a little info, it would be better to Use the MAC address of a device, there is always a sticker on a Player with the MAC 🙂 just in case you have more than one Sonos Player…
Appreciate your comment 🙂 I’ll keep it in mind, while extending the script.
You should be able to see the IP address in your wireless router as well. Most units have a page to show you connected devices. Looking forward to trying this out.
You can also use the regular Sonos Controller to retrieve the IP Address of your Device(s). (Help -> About My Sonos System)
I’ve started playing with an Invoke-Webrequest version of your code. https://gist.github.com/jdhitsolutions/99c843e0250963dca90a
Wow, that is awesome. Can I use this for further developments? I will of course, tag you as contributor / author in the script.
You really make it appear really easy with your presentation but I in finding this topic to be actually
one thing that I believe I’d by no means understand.
It kind of feels too complex and extremely extensive for me.
I am looking ahead in your next put up, I’ll attempt to get the dangle of it!
Hi thnaks for the script!
, did some packet trace also and found out how to control the LED 🙂
This is how to turn it off:
$soapMessage = ‘
Change OFF to ON to turn it on.
Just FYI 🙂