Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active March 1, 2022 13:09
Show Gist options
  • Save jdhitsolutions/99c843e0250963dca90a to your computer and use it in GitHub Desktop.
Save jdhitsolutions/99c843e0250963dca90a to your computer and use it in GitHub Desktop.
this is a variation of a script posted at https://gallery.technet.microsoft.com/SONOS-PowerShell-500c9878 that uses Invoke-Webrequest.
<#
this is a variation of a script posted at
https://gallery.technet.microsoft.com/SONOS-PowerShell-500c9878
that uses Invoke-Webrequest.
This is very much a work in progress
other resources
http://musicpartners.sonos.com/?q=node/224#toc4
http://musicpartners.sonos.com/node/134
http://www.programmableweb.com/api/sonos-music/source-code
http://stackoverflow.com/questions/tagged/sonos
#>
[cmdletbinding()]
Param(
[Parameter(Mandatory)]
[ValidateSet("Play","Pause","Info")]
[String]$Action,
[ValidateNotNullorEmpty()]
[string]$ip = "172.16.10.179",
[ValidateNotNullorEmpty()]
[int]$port = 1400
)
#Info doesn't work yet because I have to get and parse the response
Switch ($Action) {
"Pause" { $soapAction = @{
"path" = "/MediaRenderer/AVTransport/Control"
"soapAction" = "urn:schemas-upnp-org:service:AVTransport:1#Pause"
"message" = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Pause xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID></u:Pause></s:Body></s:Envelope>'
}
}
"Play" { $soapAction = @{
path = "/MediaRenderer/AVTransport/Control"
soapAction = "urn:schemas-upnp-org:service:AVTransport:1#Play"
message = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:Play xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID><Speed>1</Speed></u:Play></s:Body></s:Envelope>'
}
}
"Info" {
$soapAction = @{
path = "/MediaRenderer/AVTransport/Control"
soapAction = "urn:schemas-upnp-org:service:AVTransport:1#GetPositionInfo"
message = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetPositionInfo xmlns:u="urn:schemas-upnp-org:service:AVTransport:1"><InstanceID>0</InstanceID></u:GetPositionInfo></s:Body></s:Envelope>'
}
}
}
$uri = "http://$($ip):$port$($soapaction.path)"
Write-Verbose "Connecting to $uri"
Write-Verbose "Action = $($soapAction.soapAction)"
# Set Headers
$headers = @{
'Accept-Encoding' = 'gzip'
SOAPACTION = $soapAction.soapAction
}
Write-Verbose "Headers"
Write-Verbose ($headers | out-string)
[xml]$body = $soapAction.message
Write-Verbose "Body = $($soapaction.message)"
$paramHash = @{
Uri = $uri
Headers = $headers
ContentType = 'text/xml; charset="UTF-8"'
DisableKeepAlive = $True
Method = 'Post'
Body = $body
UseBasicParsing = $True
ErrorAction = "Stop"
}
Write-Verbose "Using param hash"
Write-Verbose ($paramHash | out-string)
Try {
Invoke-WebRequest @paramHash
}
Catch {
$_ | select *
<#
if ($_.Exception.gettype().name -ne 'xArgumentNullException') {
$_
}
#>
}
@jdhitsolutions
Copy link
Author

I have hardcoded the IP address of one of my Sonos units. You'll of course need to change that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment