Send-Roku.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
function Send-Roku { <# .Synopsis Sends to the Roku .Description Sends REST messages to the Roku External Command Protocol. or Sends a keyboard sequence to a Roku. .Link Get-Roku .Example Send-Roku -IPAddress $myRokuIP -Command query/device-info #> [CmdletBinding(SupportsShouldProcess,ConfirmImpact='Low',DefaultParameterSetName='Command')] [OutputType([xml], [Nullable])] param( # The IP Address of the Roku. [Parameter(ValueFromPipelineByPropertyName)] [IPAddress[]] $IPAddress, # The URI fragment to send to the Roku. [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='Command')] [string] $Command, # The HTTP method to send. [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='Command')] [string] $Method = 'GET', # The text to send. Each character will be turned into a literal and sent to the Roku. [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='Text')] [string] $Text, # Will send a single KeyDown message to the Roku, similar to holding a key down. [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='keydown')] [string] $KeyDown, # Will send a single KeyUp message to the Roku, releasing a key that has been held down. [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='keyup')] [string] $KeyUp, # Will send a single keypress to the Roku. [Parameter(Mandatory,ValueFromPipelineByPropertyName,ParameterSetName='keypress')] [string] $KeyPress, # The data to send. This will be converted into JSON if it is not a string. [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='Command')] [PSObject] $Data, # If set, will attempt to mute the device. [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='VolumeMute')] [switch] $Mute, # If set, will raise the volume. [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='VolumeUp')] [switch] $VolumeUp, # If set, will lower the volume. [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='VolumeDown')] [switch] $VolumeDown, # If set, will switch to the TV tuner [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='launch/tvinput.dtv')] [switch] $TVTuner, # If provided, will change the channel on the TV Tuner. [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='launch/tvinput.dtv')] [double] $Channel, # If provided, will change the TV Tuner to a given HDMI input. [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='launch/tvinput')] [ValidateRange(1,10)] [uint32] $HDMIInput, # If set, will change the Roku TV to use it's AV input [Parameter(ValueFromPipelineByPropertyName,ParameterSetName='launch/tvinput')] [switch] $AVInput, # A set of additional properties to add to an object [Parameter(ValueFromPipelineByPropertyName)] [Collections.IDictionary] $Property = @{}, # A list of property names to remove from an object [string[]] $RemoveProperty, # If provided, will expand a given property returned from the REST api. [string] $ExpandProperty, # The typename of the results. [Parameter(ValueFromPipelineByPropertyName)] [string[]] $PSTypeName ) process { #region Broadcast Recursively if no -IPAddress was provided if (-not $IPAddress -or $IPAddress -eq [IPAddress]::Broadcast -or $IPAddress -eq [IPAddress]::Any ) { if (-not $script:CachedDiscoveredRokus) { Find-Roku | Out-Null } if ($script:CachedDiscoveredRokus) { $IPAddress = $script:CachedDiscoveredRokus | Select-Object -ExpandProperty IPAddress } else { Write-Error "No Rokus found" return } } $psParameterSet = $PSCmdlet.ParameterSetName :nextIPAddress foreach ($ip in $ipaddress) { if ($psParameterSet -eq 'Text') { foreach ($char in $text.ToCharArray()) { $u = [Web.HttpUtility]::UrlEncode("$char") Send-Roku -Command "keypress/Lit_$u" -IPAddress $ip -Method POST -Data '' | & { process { if ($WhatIfPreference) {$_ } } } } continue nextIPAddress } if ($psParameterSet -like "Volume*") { $KeyPress = "$psParameterSet" $psParameterSet = 'keypress' } if ($psParameterSet -like 'key*') { $key = $KeyDown, $KeyUp, $KeyPress -ne '' Send-Roku -Command "$psParameterSet/$key" -IPAddress $ip -Method POST -Data '' | & { process { if ($WhatIfPreference) {$_ } } } continue nextIPAddress } if ($TVTuner -or $Channel) { Send-Roku -Command "$psParameterSet$(if ($Channel) {"?ch=$($channel)"})" -IPAddress $ip -Method POST -Data '' | & { process { if ($WhatIfPreference) {$_ } } } continue nextIPAddress } if ($psParameterSet -eq 'launch/tvinput') { $command = "$psParameterSet" if ($AVInput) { $Command += ".cvbs" } else { $Command += ".hdmi$($HDMIInput)" } Send-Roku -Command $Command -IPAddress $ip -Method POST -Data '' | & { process { if ($WhatIfPreference) {$_ } } } continue nextIPAddress } $splat = @{ uri = "http://${ip}:8060/$Command" method = $Method } if ($Data) { if ($data -is [string]){ $splat.body = $data } else { $splat.body = ConvertTo-Json -Compress -Depth 10 -InputObject $Data } } if ($WhatIfPreference) { $splat continue nextIPAddress } if (-not $property) { $property = [Ordered]@{} } $property['IPAddress'] = $ip $psProperties = @( foreach ($propKeyValue in $Property.GetEnumerator()) { if ($propKeyValue.Value -as [ScriptBlock[]]) { [PSScriptProperty]::new.Invoke(@($propKeyValue.Key) + $propKeyValue.Value) } else { [PSNoteProperty]::new($propKeyValue.Key, $propKeyValue.Value) } } ) if (! $PSCmdlet.ShouldProcess("$Method $($splat.Uri)")) { continue } if ($WhatIfPreference) { continue nextIPAddress } Invoke-RestMethod @splat 2>&1 | & { process { $in = $_ if ($in -isnot [xml]) { $inXml = $in -as [xml] if ($inXml) { $in = $inXml } } if (-not $in -or $in -eq 'null') { return } if ($ExpandProperty) { if ($in.$ExpandProperty) { $in.$ExpandProperty } } else { $in # pass it down the pipe. } } } 2>&1 | & { process { # One more step of the pipeline will unroll each of the values. if ($_ -is [string]) { return $_ } if ($null -ne $_.Count -and $_.Count -eq 0) { return } $in = $_ if ($PSTypeName -and # If we have a PSTypeName (to apply formatting) $in -isnot [Management.Automation.ErrorRecord] # and it is not an error (which we do not want to format) ) { $in.PSTypeNames.Clear() # then clear the existing typenames and decorate the object. foreach ($t in $PSTypeName) { $in.PSTypeNames.add($T) } } if ($Property -and $Property.Count) { foreach ($prop in $psProperties) { $in.PSObject.Members.Add($prop, $true) } } if ($RemoveProperty) { foreach ($propToRemove in $RemoveProperty) { $in.PSObject.Properties.Remove($propToRemove) } } if ($DecorateProperty) { foreach ($kv in $DecorateProperty.GetEnumerator()) { if ($in.$($kv.Key)) { foreach ($v in $in.$($kv.Key)) { if ($null -eq $v -or -not $v.pstypenames) { continue } $v.pstypenames.clear() foreach ($tn in $kv.Value) { $v.pstypenames.add($tn) } } } } } return $in # output the object and we're done. } } foreach ($ir in $invokeResult) { $ir.psobject.properties.add($ipNoteProperty) $ir } } } } |