Get-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
function Get-Roku
{
    <#
    .Synopsis
        Gets Rokus
    .Description
        Gets Rokus and information from Rokus.
    .Link
        Find-Roku
    .Example
        Get-Roku # Get basic info
    .Example
        Get-Roku -App # Get Roku apps
    .Example
        Get-Roku -Screensaver # Get the Roku media player
    #>

    [CmdletBinding(DefaultParameterSetName='query/device-info')]
    [OutputType([PSObject])]
    param(
    # The IP Address of the Roku. If not provided, all discovered rokus will be contacted.
    [Parameter(ValueFromPipelineByPropertyName)]
    [IPAddress[]]
    $IPAddress,

    # If set, will get device information about the Roku.
    [Parameter(ParameterSetName='query/device-info')]
    [switch]
    $DeviceInfo,

    # If set, will get apps from the Roku.
    [Parameter(Mandatory,ParameterSetName='query/apps')]
    [Alias('Apps')]
    [switch]
    $App,

    # If set, will get the Active App on the Roku.
    [Parameter(Mandatory,ParameterSetName='query/active-app')]
    [switch]
    $ActiveApp,

    # If set, get Screensavers installed on the Roku.
    [Parameter(Mandatory,ParameterSetName='query/screensavers')]
    [Alias('Screensavers')]
    [switch]
    $Screensaver,

    # If set, get Screensavers installed on the Roku.
    [Parameter(Mandatory,ParameterSetName='query/themes')]
    [Alias('Themes')]
    [switch]
    $Theme,

    # If set, will get tv channels from the Roku.
    # This is only supported for Roku TVs.
    [Parameter(Mandatory,ParameterSetName='query/tv-channels')]
    [Alias('Channel', 'Channels','TVChannels')]
    [switch]
    $TVChannel,

    # If set, will get the active channel from the Roku.
    # This is only supported for Roku TVs.
    [Parameter(Mandatory,ParameterSetName='query/tv-active-channel')]
    [switch]
    $ActiveTVChannel,

    # If set, will get media player information from the Roku.
    [Parameter(Mandatory,ParameterSetName='query/media-player')]
    [switch]
    $MediaPlayer,

    # If set, will list Rokus that have already been discovered with Find-Roku.
    # If no Rokus have been discovered, it will attempt to find them.
    [Parameter(Mandatory,ParameterSetName='KnownRokus')]
    [Alias('Discovered')]
    [switch]
    $Discover
    )

    begin {
        filter decorate([string[]]$Typename) {
            $_.pstypenames.clear()
            foreach ($tn in $Typename) {
                $_.pstypenames.add($tn)
            }
            $_
        }
    }

    process {
        $psParameterSet = $PSCmdlet.ParameterSetName
        $isQuery, $thingWeQuery = $PSCmdlet.ParameterSetName -split '/'
        if ($isQuery -eq 'query' -and $thingWeQuery) {
            #region Querying for Data
            if (-not $IPAddress -or
                $IPAddress -eq [IPAddress]::Broadcast) {
                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
                }
            }

            foreach ($ip in $IPAddress) {
                $queryData = Send-Roku -IPAddress $ip -Command $PSCmdlet.ParameterSetName
                switch ($thingWeQuery) {
                    'apps' {
                        $queryData |
                            Select-Object -ExpandProperty Apps |
                            Select-Object -ExpandProperty App |
                            decorate Roku.App
                    }
                    'themes' {
                        $queryData |
                            Select-Object -ExpandProperty Themes |
                            Select-Object -ExpandProperty Theme |
                            ForEach-Object {
                                [PSCustomObject][Ordered]@{
                                    Name = $_.'#text'
                                    Id   = $_.id
                                    Selected = if ($_.'selected') { $true } else { $false }
                                }
                            } |
                            decorate Roku.Theme

                    }
                    'screensavers' {
                        $queryData |
                            Select-Object -ExpandProperty Screensavers |
                            Select-Object -ExpandProperty Screensaver |
                            decorate Roku.Screensaver, Roku.App
                    }
                    'media-player' {
                        $queryData |
                            Select-Object -ExpandProperty Player |
                            decorate "Roku.MediaPlayer"
                    }
                    'tv-channels' {
                        $queryData |
                            Select-Object -ExpandProperty tv-channels |
                            Select-Object -ExpandProperty channel |
                            decorate "Roku.TV.Channel"
                    }
                    'device-info' {
                        $queryData |
                            Select-Object -ExpandProperty device-info |
                            Add-Member NoteProperty IPAddress $IPAddress -Force -PassThru |
                            decorate $(
                                if ($DeviceInfo) {
                                    "Roku.Device"
                                } else {
                                    "Roku.Device#Basic", "Roku.Device"
                                })
                    }
                    default {
                        $queryData
                    }
                }
            }
            #endregion Querying for Data
        }
        elseif ($psParameterSet -eq 'KnownRokus') {
            #region Output known Rokus
            Find-Roku # If this has already run, it will return the cached rokus.
            #endregion Output known Rokus
        }
    }
}