-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediabutler.ps1
2394 lines (2339 loc) · 88.6 KB
/
mediabutler.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Requires -Version 5.0
# Script to setup/configure MediaButler
# HalianElf
using namespace System.Management.Automation
[CmdletBinding()]
param(
[parameter (
Mandatory=$false
, position=0
, HelpMessage="Enable debug output"
)
]
[Switch]$DebugOn = $false
)
# Define variables
$InformationPreference = 'Continue'
$Host.UI.RawUI.BackgroundColor = 'Black'
[Net.ServicePointManager]::SecurityProtocol = "Tls12, Tls11, Tls, Ssl3"
$uuid = "fb67fb8b-9000-4a70-a67b-2f2b626780bb"
$userDataPath = '.\userData.json'
$plexLoginURL = "https://plex.tv/users/sign_in.json"
$mbLoginURL = "https://auth.mediabutler.app/login"
$mbDiscoverURL = "https://auth.mediabutler.app/login/discover"
$userData = @{}
$setupChecks = @{
"sonarr"=$false;
"sonarr4k"=$false;
"radarr"=$false;
"radarr4k"=$false;
"radarr3d"=$false;
"tautulli"=$false;
}
$isAdmin = $false
Clear-Host
if ($DebugOn) {
$DebugPreference = 'Continue'
}
# Function to change the color output of text
# https://blog.kieranties.com/2018/03/26/write-information-with-colours
function Write-ColorOutput() {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[Object]$MessageData,
[ConsoleColor]$ForegroundColor = $Host.UI.RawUI.ForegroundColor, # Make sure we use the current colours by default
[ConsoleColor]$BackgroundColor = $Host.UI.RawUI.BackgroundColor,
[Switch]$NoNewline
)
$msg = [HostInformationMessage]@{
Message = $MessageData
ForegroundColor = $ForegroundColor
BackgroundColor = $BackgroundColor
NoNewline = $NoNewline.IsPresent
}
Write-Information $msg
}
# Check if userData.json exists
# Returns the array of the User Data
function checkUserData() {
if (Test-Path $userDataPath -PathType Leaf) {
$fileIn = Get-Content -Raw -Path $userDataPath | ConvertFrom-Json
$fileIn.psobject.properties | Foreach-Object { $userData[$_.Name] = $_.Value }
}
}
# Check if Plex Auth Token is saved in userData and if not print menu and get it from user
function checkPlexAuth() {
if ([string]::IsNullOrEmpty($userData.authToken)) {
do {
Write-Information ""
Write-ColorOutput -ForegroundColor gray -MessageData "Welcome to the MediaButler Windows CLI Utility!"
Write-Information ""
Write-ColorOutput -ForegroundColor gray -MessageData "First thing we need to get started are your Plex credentials."
Write-ColorOutput -ForegroundColor gray -MessageData "Please choose from one of the following options:"
Write-Information ""
Write-ColorOutput -nonewline -MessageData " 1) "; Write-ColorOutput -ForegroundColor gray -MessageData "Plex Username and Password"
Write-ColorOutput -nonewline -MessageData " 2) "; Write-ColorOutput -ForegroundColor gray -MessageData "Plex token"
Write-ColorOutput -nonewline -MessageData " 3) "; Write-ColorOutput -ForegroundColor gray -MessageData "Exit"
Write-Information ""
$valid = $false
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Selection: "
$ans = Read-Host
try {
$ans = [int]$ans
} catch {
[int]$ans = 0
}
if ($ans -eq 1) {
$userData.authToken = plexLogin
$mbLoginResponse = mbLogin $userdata.authToken
$valid = $true
} elseif ($ans -eq 2) {
do {
Write-Information ""
Write-ColorOutput -ForegroundColor gray -MessageData "Please enter your Plex token:"
$authTokenEnc = Read-Host -AsSecureString
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList "authToken", $authTokenEnc
$userdata.authToken = $credentials.GetNetworkCredential().Password
$mbLoginResponse = mbLogin $userdata.authToken
Write-Information ""
Write-ColorOutput -ForegroundColor gray -MessageData "Now we're going to make sure you provided valid credentials..."
if([string]::IsNullOrEmpty($mbLoginResponse)) {
Write-ColorOutput -ForegroundColor red -MessageData "The credentials that you provided are not valid!"
} else {
Write-ColorOutput -ForegroundColor green -MessageData "Success!"
}
} while ([string]::IsNullOrEmpty($mbLoginResponse))
$valid = $true
} elseif ($ans -eq 3) {
Clear-Host
Exit
} else {
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "Invalid Response. Please try again."
}
} while (-Not ($valid))
$userData | ConvertTo-Json | Out-File -FilePath $userDataPath
}
}
# Function for logging into Plex with a Username/Password
# This will loop until valid credentials are provided
# Returns Plex authToken
function plexLogin() {
$authToken = ""
do {
try {
# Reset variables
$failedLogin = $false
$err = ""
# Prompt for Username/Password
Write-Information ""
Write-ColorOutput -ForegroundColor gray -MessageData "Please enter your Plex username:"
$plexusername = Read-Host
Write-Information ""
Write-ColorOutput -ForegroundColor gray -MessageData "Please enter your Plex password:"
$plexpassword = Read-Host -AsSecureString
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $plexusername, $plexpassword
$plexptpasswd = $credentials.GetNetworkCredential().Password
# Format requests in JSON for API Call
$headers = @{
"X-Plex-Device"="API";
"X-Plex-Device-Name"="DeviceName";
"X-Plex-Product"="MediaButler";
"X-Plex-Version"="v1.0";
"X-Plex-Platform-Version"="v1.0";
"X-Plex-Client-Identifier"="df9e71a5-a6cd-488e-8730-aaa9195f7435";
};
$creds = @{
"login"="$plexusername";
"password"="$plexptpasswd";
};
$body = @{
"user"=$creds;
"json"="true";
};
$body = $body | ConvertTo-Json
Write-Information ""
Write-ColorOutput -ForegroundColor gray -MessageData "Now we're going to make sure you provided valid credentials..."
$response = Invoke-WebRequest -Uri $plexLoginURL -Method POST -Headers $headers -Body $body -ContentType "application/json" -TimeoutSec 10 -UseBasicParsing
$response = $response | ConvertFrom-Json
$authToken = $response.user.authToken
Write-ColorOutput -ForegroundColor green -MessageData "Success!"
} catch [System.Net.WebException] {
$err = $_.Exception.Response.StatusCode
$failedLogin = $true
if ($err -eq "Unauthorized") {
Write-ColorOutput -ForegroundColor red -MessageData "The credentials that you provided are not valid!"
}
}
} while ($failedLogin)
$authToken
}
# Checks if provided authToken is valid
# Returns the response from MediaButler Login (empty string if failed)
function mbLogin($authToken) {
$response = ""
try {
# Auth with PlexToken to MediaButler
$headers = @{
"MB-Client-Identifier"=$uuid;
};
$body = @{
"authToken"=$authToken;
};
$body = $body | ConvertTo-Json
$response = Invoke-WebRequest -Uri $mbLoginURL -Method POST -Headers $headers -Body $body -ContentType "application/json" -TimeoutSec 10 -UseBasicParsing
# Convert to Array so it can be used
$response = $response | ConvertFrom-Json
$response
} catch [System.Net.WebException] {
Write-Debug $_.Exception.Message
$response
}
}
# Choose Server for MediaButler
function chooseServer() {
if (([string]::IsNullOrEmpty($userData.machineId)) -Or ([string]::IsNullOrEmpty($userData.mbToken))) {
if ([string]::IsNullOrEmpty($mbLoginResponse)) {
$mbLoginResponse = mbLogin $userdata.authToken
}
# Print Owned Servers and create menu with them
$i = 0
Write-Information ""
Write-ColorOutput -ForegroundColor gray -MessageData "Please choose which Plex Server you would like to setup MediaButler for:"
Write-Information ""
$menu = @{}
foreach ($server in $mbLoginResponse.servers) {
try {
$owner = [System.Convert]::ToBoolean($server.owner)
} catch [FormatException] {
$owner = $false
}
$i++
Write-ColorOutput -nonewline -MessageData " $i) "; Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "$($server.name)"
if ($owner) {
Write-ColorOutput -ForegroundColor gray -MessageData " (Owner)"
} else {
Write-Information ""
}
$serverInfo = @{"serverName"="$($server.name)"; "machineId"="$($server.machineId)"; "mbToken"="$($server.token)";};
$menu.Add($i,($serverInfo))
}
Write-Information ""
do {
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Server: "
$ans = Read-Host
try {
$ans = [int]$ans
} catch {
[int]$ans = 0
}
if ($ans -ge 1 -And $ans -le $i) {
$valid = $true
$userData.serverName = $menu.Item($ans).serverName
$userData.machineId = $menu.Item($ans).machineId
$userData.mbToken = $menu.Item($ans).mbToken
} else {
$valid = $false
Write-ColorOutput -ForegroundColor red -MessageData "You did not specify a valid option!"
}
} while (-Not ($valid))
$userData | ConvertTo-Json | Out-File -FilePath $userDataPath
}
}
# Takes a MediaButler url and tests it for the API version. If that doesn't come back with an API version above 1.1.12, it's not MediaButler
# Returns $true or $false
function testMB($url) {
$isMB = $false
try {
$response = Invoke-WebRequest -Uri $url"version" -TimeoutSec 10 -UseBasicParsing
$response = $response | ConvertFrom-Json
$apiVersion = $response.apiVersion.Split(".")
if (($apiVersion[0] -gt 1) -Or ($apiVersion[1] -gt 1) -Or ($apiVersion[2] -ge 12)) {
$isMB = $true;
}
} catch {
Write-Debug $_.Exception.Message
$isMB = $false;
}
$isMB
}
# Get MediaButler URL
function getMbURL() {
if ([string]::IsNullOrEmpty($userData.mbURL)) {
# Test if localhost is MediaButler server
$isMB = $false;
$manual = $false
# Use Plex token and Machine ID to get URL
try {
Write-ColorOutput -ForegroundColor gray -MessageData "Gathering required information..."
$headers = @{
"MB-Client-Identifier"=$uuid;
"MB-Plex-Token"=$userData.authToken;
"MB-Machine-Identifier"=$userData.machineId;
};
$mbURL = Invoke-WebRequest -Uri $mbDiscoverURL -Method GET -Headers $headers -ContentType "application/json" -TimeoutSec 10 -UseBasicParsing
Write-Debug "Received URL: $mbURL"
if ([string]::IsNullOrEmpty($mbURL)) {
throw "Blank result"
}
$lastChar = $mbURL.SubString($mbURL.Length - 1)
if ($lastChar -ne "/") {
$mbURL = "$mbURL/"
}
Write-ColorOutput -ForegroundColor green -MessageData "Done!"
Write-Information ""
Write-ColorOutput -ForegroundColor gray -MessageData "Is this the correct MediaButler URL?"
Write-ColorOutput -ForegroundColor yellow -MessageData $mbURL
Write-Information ""
Write-ColorOutput -ForegroundColor green -nonewline -MessageData "[Y]"; Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "es or "; Write-ColorOutput -ForegroundColor red -nonewline -MessageData "[N]"; Write-ColorOutput -ForegroundColor gray -MessageData "o";
$valid = $false
do {
$ans = Read-Host
if (($ans -notlike "y") -And ($ans -notlike "yes") -And ($ans -notlike "n") -And ($ans -notlike "no")) {
Write-ColorOutput -ForegroundColor red -MessageData "Please specify yes, y, no, or n."
$valid = $false
} elseif (($ans -like "y") -Or ($ans -like "yes")) {
$valid = $true
} else {
$valid = $true
$manual = $true
throw "Not correct URL"
}
} while (-Not ($valid))
} catch {
if (-Not($manual)) {
# If token doesn't work, ask user and loop until a correct url is given
Write-ColorOutput -ForegroundColor red -MessageData "Unable to automatically retrieve your MediaButler URL!"
Write-ColorOutput -ForegroundColor yellow -MessageData "This is typically indicative of port 9876 not being forwarded."
Write-ColorOutput -ForegroundColor yellow -MessageData "Please check your port forwarding and try again."
}
do {
Write-ColorOutput -ForegroundColor gray -MessageData "Please enter the correct MediaButler URL:"
$mbURL = Read-Host
$lastChar = $mbURL.SubString($mbURL.Length - 1)
if ($lastChar -ne "/") {
$mbURL = "$mbURL/"
}
$isMB = testMB $mbURL;
if(-Not ($isMB)) {
Write-ColorOutput -ForegroundColor red -MessageData "Invalid Server URL"
}
} while(-Not ($isMB));
}
$userData.mbURL = [String]$mbURL
$userData | ConvertTo-Json | Out-File -FilePath $userDataPath
}
}
# Do status checks on each of the endpoints to see if they're setup and set boolean
function setupChecks() {
for ($i=0; $i -lt 6; $i++) {
switch ($i) {
0 { $endpoint = "sonarr"; break }
1 { $endpoint = "sonarr4k"; break }
2 { $endpoint = "radarr"; break }
3 { $endpoint = "radarr4k"; break }
4 { $endpoint = "radarr3d"; break }
5 { $endpoint = "tautulli"; break }
}
Write-Debug "Checking to see if $endpoint is set up"
$headers = @{
"Content-Type"="application/json"
"MB-Client-Identifier"=$uuid;
"Authorization"="Bearer " + $userData.mbToken;
};
$formattedURL = [System.String]::Concat(($userData.mbURL), 'configure/', ($endpoint))
try {
$response = Invoke-WebRequest -Uri $formattedURL -Method GET -Headers $headers -TimeoutSec 10 -UseBasicParsing
$response = $response | ConvertFrom-Json
} catch {
Write-Debug $_.Exception.Message
}
Write-Debug $response.settings
if (-Not [string]::IsNullOrEmpty($response.settings)) {
$setupChecks.($endpoint) = $true
}
}
}
# Check if logged in user is admin of the chosen server
function checkAdmin() {
$headers = @{
"Content-Type"="application/json"
"MB-Client-Identifier"=$uuid;
"Authorization"="Bearer " + $userData.mbToken;
};
$formattedURL = [System.String]::Concat(($userData.mbURL), 'user/@me/')
try {
$response = Invoke-WebRequest -Uri $formattedURL -Method GET -Headers $headers -TimeoutSec 10 -UseBasicParsing
Write-Debug $response
$response = $response | ConvertFrom-Json
if ($response.permissions -contains "ADMIN") {
$script:isAdmin = $true
}
} catch {
Write-Debug "Checking if logged in user is Admin"
Write-Debug $_.Exception.Message
if ($_.Exception.Message -like "*Invalid JSON primitive*") {
Write-ColorOutput -ForegroundColor red -MessageData "There was an issue checking your permissions for the selected Plex Server!"
Write-ColorOutput -ForegroundColor yellow -MessageData "Please make sure your MediaButler API is functioning properly and try again."
Exit
}
}
}
# Print the main menu and get input
function mainMenu() {
do {
Write-Information ""
Write-Information "+---------------------------------------+"
Write-Information "| ~Main Menu~ |"
Write-Information "+---------------------------------------+"
Write-ColorOutput -ForegroundColor gray -MessageData "Please select from the following options:"
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData " ("; Write-ColorOutput -ForegroundColor red -nonewline -MessageData "*"; Write-ColorOutput -ForegroundColor gray -MessageData " indicates Admin only) "
Write-Information ""
Write-ColorOutput -nonewline -MessageData " 1) "; Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Configure Applications"; Write-ColorOutput -ForegroundColor red -MessageData "*"
Write-ColorOutput -nonewline -MessageData " 2) "; Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Manage Permissions"; Write-ColorOutput -ForegroundColor red -MessageData "*"
Write-ColorOutput -nonewline -MessageData " 3) "; Write-ColorOutput -ForegroundColor gray -MessageData "Plex Media Requests"
Write-ColorOutput -nonewline -MessageData " 4) "; Write-ColorOutput -ForegroundColor gray -MessageData "Plex Media Issues"
Write-ColorOutput -nonewline -MessageData " 5) "; Write-ColorOutput -ForegroundColor gray -MessageData "Plex Playback Information"
Write-ColorOutput -nonewline -MessageData " 6) "; Write-ColorOutput -ForegroundColor gray -MessageData "Plex Library Information"
Write-ColorOutput -nonewline -MessageData " 7) "; Write-ColorOutput -ForegroundColor gray -MessageData "Plex Media Search"
Write-ColorOutput -nonewline -MessageData " 8) "; Write-ColorOutput -ForegroundColor gray -MessageData "Reset Config"
Write-ColorOutput -nonewline -MessageData " 9) "; Write-ColorOutput -ForegroundColor gray -MessageData "Exit"
Write-Information ""
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Selection: "
$ans = Read-Host
try {
$ans = [int]$ans
} catch {
[int]$ans = 0
}
if (-Not(($ans -ge 1) -And ($ans -le 9))) {
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "You did not specify a valid option!"
$valid = $false
} elseif ($ans -eq 1) {
if (-Not ($isAdmin)) {
$valid = $false
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "You do not have permission to access this menu!"
Start-Sleep -s 3
Clear-Host
} elseif ($isAdmin) {
$valid = $true
endpointMenu
}
} elseif ($ans -eq 2) {
$valid = $true
manageUsers
} elseif ($ans -eq 3) {
$valid = $true
requestsMenu
} elseif ($ans -eq 4) {
$valid = $true
issuesMenu
} elseif ($ans -eq 5) {
$valid = $true
playbackMenu
} elseif ($ans -eq 6) {
$valid = $true
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "Not setup yet!"
mainMenu
#libraryMenu
} elseif ($ans -eq 7) {
$valid = $true
searchMenu
} elseif ($ans -eq 8) {
$valid = $true
resetAll
} elseif ($ans -eq 9) {
$valid = $true
exitMenu
}
} while (-Not($valid))
}
# Print the Endpoint Menu and get input
function endpointMenu() {
do {
Write-Information ""
Write-Information "+---------------------------------------+"
Write-Information "| ~Endpoint Configuration Menu~ |"
Write-Information "+---------------------------------------+"
Write-ColorOutput -ForegroundColor gray -MessageData "Please choose which application you would"
Write-ColorOutput -ForegroundColor gray -MessageData " like to configure for MediaButler: "
Write-Information ""
Write-ColorOutput -nonewline -MessageData " 1) "
if ($setupChecks.sonarr -And $setupChecks.sonarr4k) {
Write-ColorOutput -ForegroundColor green -MessageData "Sonarr"
} elseif ($setupChecks.sonarr -Or $setupChecks.sonarr4k) {
Write-ColorOutput -ForegroundColor yellow -MessageData "Sonarr"
} else {
Write-ColorOutput -ForegroundColor red -MessageData "Sonarr"
}
Write-ColorOutput -nonewline -MessageData " 2) "
if ($setupChecks.radarr -And $setupChecks.radarr4k -And $setupChecks.radarr3d) {
Write-ColorOutput -ForegroundColor green -MessageData "Radarr"
} elseif ($setupChecks.radarr -Or $setupChecks.radarr4k -Or $setupChecks.radarr3d) {
Write-ColorOutput -ForegroundColor yellow -MessageData "Radarr"
} else {
Write-ColorOutput -ForegroundColor red -MessageData "Radarr"
}
Write-ColorOutput -nonewline -MessageData " 3) "
if ($setupChecks.tautulli) {
Write-ColorOutput -ForegroundColor green -MessageData "Tautulli"
} else {
Write-ColorOutput -ForegroundColor red -MessageData "Tautulli"
}
Write-ColorOutput -nonewline -MessageData " 4) "; Write-ColorOutput -ForegroundColor gray -MessageData "Requests"
Write-ColorOutput -nonewline -MessageData " 5) "; Write-ColorOutput -ForegroundColor gray -MessageData "Return to Main Menu"
Write-Information ""
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Selection: "
$ans = Read-Host
try {
$ans = [int]$ans
} catch {
[int]$ans = 0
}
if (-Not(($ans -ge 1) -And ($ans -le 5))) {
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "You did not specify a valid option!"
$valid = $false
} elseif ($ans -eq 1) {
$valid = $true
sonarrMenu
} elseif ($ans -eq 2) {
$valid = $true
radarrMenu
} elseif ($ans -eq 3) {
$valid = $true
setupTautulli
} elseif ($ans -eq 4) {
$valid = $true
configRequests
} elseif ($ans -eq 5) {
$valid = $true
Clear-Host
mainMenu
}
} while (-Not($valid))
}
# Print the Requests Menu and get input
function requestsMenu() {
do {
Write-Information ""
Write-Information "+---------------------------------------+"
Write-Information '| ~Plex Requests Menu~ |'
Write-Information "+---------------------------------------+"
Write-ColorOutput -ForegroundColor gray -MessageData "Please select from the following options:"
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData " ("; Write-ColorOutput -ForegroundColor red -nonewline -MessageData "*"; Write-ColorOutput -ForegroundColor gray -MessageData " indicates Admin only) "
Write-Information ""
Write-ColorOutput -nonewline -MessageData " 1) "; Write-ColorOutput -ForegroundColor gray -MessageData "Submit Request"
Write-ColorOutput -nonewline -MessageData " 2) "; Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Manage Requests"; Write-ColorOutput -ForegroundColor red "*"
Write-ColorOutput -nonewline -MessageData " 3) "; Write-ColorOutput -ForegroundColor gray -MessageData "Back to Main Menu"
Write-Information ""
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Selection: "
$ans = Read-Host
try {
$ans = [int]$ans
} catch {
[int]$ans = 0
}
if (-Not(($ans -ge 1) -And ($ans -le 3))) {
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "You did not specify a valid option!"
$valid = $false
} elseif ($ans -eq 1) {
$valid = $true
submitRequest
} elseif ($ans -eq 2) {
if (-Not ($isAdmin)) {
$valid = $false
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "You do not have permission to access this menu!"
Start-Sleep -s 3
Clear-Host
mainMenu
} elseif ($isAdmin) {
$valid = $true
manageRequests
}
} elseif ($ans -eq 3) {
$valid = $true
Clear-Host
mainMenu
}
} while (-Not($valid))
}
# Print the Issues menu and get input
function issuesMenu() {
do {
Write-Information ""
Write-Information "+---------------------------------------+"
Write-Information '| ~Plex Media Issues Menu~ |'
Write-Information "+---------------------------------------+"
Write-ColorOutput -ForegroundColor gray -MessageData "Please select from the following options:"
Write-ColorOutput -nonewline -ForegroundColor gray -MessageData " ("; Write-ColorOutput -ForegroundColor red -nonewline -MessageData "*"; Write-ColorOutput -ForegroundColor gray -MessageData " indicates Admin only) "
Write-Information ""
Write-ColorOutput -nonewline -MessageData " 1) "; Write-ColorOutput -ForegroundColor gray -MessageData "Add Issue"
Write-ColorOutput -nonewline -MessageData " 2) "; Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Manage Issues"; Write-ColorOutput -ForegroundColor red "*"
Write-ColorOutput -nonewline -MessageData " 3) "; Write-ColorOutput -ForegroundColor gray -MessageData "Back to Main Menu"
Write-Information ""
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Selection: "
$ans = Read-Host
try {
$ans = [int]$ans
} catch {
[int]$ans = 0
}
if (-Not(($ans -ge 1) -And ($ans -le 3))) {
Write-ColorOutput -ForegroundColor red -MessageData "You did not specify a valid option!"
$valid = $false
} elseif ($ans -eq 1) {
$valid = $true
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "Not setup yet!"
Start-Sleep -s 3
Clear-Host
mainMenu
#addIssue
} elseif ($ans -eq 2) {
if (-Not ($isAdmin)) {
$valid = $false
Write-ColorOutput -ForegroundColor red -MessageData "You do not have permission to access this menu!"
Start-Sleep -s 3
Clear-Host
mainMenu
} elseif ($isAdmin) {
$valid = $true
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "Not setup yet!"
Start-Sleep -s 3
Clear-Host
mainMenu
#manageIssues
}
} elseif ($ans -eq 3) {
$valid = $true
Clear-Host
mainMenu
}
} while (-Not($valid))
}
# Print the Playback Menu and get input
function playbackMenu() {
do {
Write-Information ""
Write-Information "+---------------------------------------+"
Write-Information '| ~Plex Playback Menu~ |'
Write-Information "+---------------------------------------+"
Write-ColorOutput -ForegroundColor gray -MessageData "Please select from the following options:"
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData " ("; Write-ColorOutput -ForegroundColor red -nonewline -MessageData "*"; Write-ColorOutput -ForegroundColor gray -MessageData " indicates Admin only) "
Write-Information ""
Write-ColorOutput -nonewline -MessageData " 1) "; Write-ColorOutput -ForegroundColor gray -MessageData "Playback History"
Write-ColorOutput -nonewline -MessageData " 2) "; Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Now Playing"; Write-ColorOutput -ForegroundColor red "*"
Write-ColorOutput -nonewline -MessageData " 3) "; Write-ColorOutput -ForegroundColor gray -MessageData "Back to Main Menu"
Write-Information ""
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Selection: "
$ans = Read-Host
try {
$ans = [int]$ans
} catch {
[int]$ans = 0
}
if (-Not(($ans -ge 1) -And ($ans -le 3))) {
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "You did not specify a valid option!"
$valid = $false
} elseif ($ans -eq 1) {
$valid = $true
playbackHistory
} elseif ($ans -eq 2) {
if (-Not ($isAdmin)) {
$valid = $false
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "You do not have permission to access this menu!"
Start-Sleep -s 3
Clear-Host
mainMenu
} elseif ($isAdmin) {
$valid = $true
nowPlaying
}
} elseif ($ans -eq 3) {
$valid = $true
Clear-Host
mainMenu
}
} while (-Not($valid))
}
# Print the Search Menu and get input
function searchMenu() {
do {
Write-Information ""
Write-Information "+---------------------------------------+"
Write-Information '| ~Plex Media Search Menu~ |'
Write-Information "+---------------------------------------+"
Write-ColorOutput -ForegroundColor gray -MessageData "Please select from the following options:"
Write-Information ""
Write-ColorOutput -nonewline -MessageData " 1) "; Write-ColorOutput -ForegroundColor gray -MessageData "TV Shows"
Write-ColorOutput -nonewline -MessageData " 2) "; Write-ColorOutput -ForegroundColor gray -MessageData "Movies"
Write-ColorOutput -nonewline -MessageData " 3) "; Write-ColorOutput -ForegroundColor gray -MessageData "Music"
Write-ColorOutput -nonewline -MessageData " 4) "; Write-ColorOutput -ForegroundColor gray -MessageData "Everything"
Write-ColorOutput -nonewline -MessageData " 5) "; Write-ColorOutput -ForegroundColor gray -MessageData "Back to Main Menu"
Write-Information ""
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Selection: "
$ans = Read-Host
try {
$ans = [int]$ans
} catch {
[int]$ans = 0
}
if (-Not(($ans -ge 1) -And ($ans -le 5))) {
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "You did not specify a valid option!"
$valid = $false
} elseif ($ans -eq 1) {
$valid = $true
#Write-Information ""
#Write-ColorOutput -ForegroundColor red -MessageData "Not setup yet!"
#Start-Sleep -s 3
#Clear-Host
#mainMenu
searchAll($ans)
} elseif ($ans -eq 2) {
$valid = $true
#Write-Information ""
#Write-ColorOutput -ForegroundColor red -MessageData "Not setup yet!"
#Start-Sleep -s 3
#Clear-Host
#mainMenu
searchAll($ans)
} elseif ($ans -eq 3) {
$valid = $true
#Write-Information ""
#Write-ColorOutput -ForegroundColor red -MessageData "Not setup yet!"
#Start-Sleep -s 3
#Clear-Host
#mainMenu
searchAll($ans)
} elseif ($ans -eq 4) {
$valid = $true
searchAll($ans)
} elseif ($ans -eq 5) {
$valid = $true
Clear-Host
mainMenu
}
} while (-Not($valid))
}
function userMgmtMenu($username) {
do {
Write-Information ""
Write-Information "+---------------------------------------+"
Write-Information '| ~Manage Permissions Menu~ |'
Write-Information "+---------------------------------------+"
Write-ColorOutput -ForegroundColor gray -MessageData "Please select from the following options:"
Write-Information ""
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Currently selected user: "; Write-ColorOutput -ForegroundColor yellow -MessageData $username
Write-Information ""
Write-ColorOutput -nonewline -MessageData " 1) "; Write-ColorOutput -ForegroundColor gray -MessageData "Add Permissions"
Write-ColorOutput -nonewline -MessageData " 2) "; Write-ColorOutput -ForegroundColor gray -MessageData "Remove Permissions"
Write-ColorOutput -nonewline -MessageData " 3) "; Write-ColorOutput -ForegroundColor gray -MessageData "Reset User"
Write-ColorOutput -nonewline -MessageData " 4) "; Write-ColorOutput -ForegroundColor gray -MessageData "Select Another User"
Write-ColorOutput -nonewline -MessageData " 5) "; Write-ColorOutput -ForegroundColor gray -MessageData "Back to Main Menu"
Write-Information ""
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Selection: "
$ans = Read-Host
try {
$ans = [int]$ans
} catch {
[int]$ans = 0
}
if (-Not(($ans -ge 1) -And ($ans -le 5))) {
Write-ColorOutput -ForegroundColor red -MessageData "You did not specify a valid option!"
$valid = $false
} elseif ($ans -eq 1) {
$valid = $true
addPerms $username
} elseif ($ans -eq 2) {
$valid = $true
remPerms $username
} elseif ($ans -eq 3) {
$valid = $true
resetPerms $username
} elseif ($ans -eq 4) {
$valid = $true
manageUsers
} elseif ($ans -eq 5) {
$valid = $true
Clear-Host
mainMenu
}
} while (-Not($valid))
}
# Print the Exit Menu
function exitMenu() {
do {
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "This will exit the program and any unfinished config setup will be lost."
Write-ColorOutput -ForegroundColor yellow -MessageData "Are you sure you wish to exit?"
Write-Information ""
Write-ColorOutput -ForegroundColor green -nonewline -MessageData "[Y]"; Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "es or "; Write-ColorOutput -ForegroundColor red -nonewline -MessageData "[N]"; Write-ColorOutput -ForegroundColor gray -MessageData "o";
$ans = Read-Host
if (($ans -notlike "y") -And ($ans -notlike "yes") -And ($ans -notlike "n") -And ($ans -notlike "no")) {
Write-ColorOutput -ForegroundColor red -MessageData "Please specify yes, y, no, or n."
$valid = $false
} elseif (($ans -like "n") -Or ($ans -like "no")) {
$valid = $true
Clear-Host
mainMenu
} else {
Clear-Host
Exit
}
} while (-Not ($valid))
}
# Delete userData.json
function resetAll() {
do {
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "**WARNING!!!** This will reset ALL setup progress!"
Write-ColorOutput -ForegroundColor yellow -MessageData "Do you wish to continue?"
Write-Information ""
Write-ColorOutput -ForegroundColor green -nonewline -MessageData "[Y]"; Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "es or "; Write-ColorOutput -ForegroundColor red -nonewline -MessageData "[N]"; Write-ColorOutput -ForegroundColor gray -MessageData "o";
$answ = Read-Host
if (($answ -notlike "y") -And ($answ -notlike "yes") -And ($answ -notlike "n") -And ($answ -notlike "no")) {
Write-ColorOutput -ForegroundColor red -MessageData "Please specify yes, y, no, or n."
$valid = $false
} elseif (($answ -like "y") -Or ($answ -like "yes")) {
$valid = $true
Remove-Item $userDataPath
Write-Information ""
Write-ColorOutput -ForegroundColor green -MessageData "The saved User Data has been removed."
Write-ColorOutput -ForegroundColor yellow -MessageData "Exiting the script..."
Start-Sleep -s 3
Clear-Host
Exit
} elseif (($answ -like "n") -Or ($answ -like "no")) {
$valid = $true
mainMenu
}
} while (-Not($valid))
}
# Print the Sonarr menu and get response
function sonarrMenu() {
do {
Write-Information ""
Write-Information "+---------------------------------------+"
Write-Information "| Sonarr Setup Menu |"
Write-Information "+---------------------------------------+"
Write-ColorOutput -ForegroundColor gray -MessageData "Please choose which version of Sonarr you"
Write-ColorOutput -ForegroundColor gray -MessageData "would like to configure for MediaButler: "
Write-Information ""
Write-ColorOutput -nonewline -MessageData " 1) "
if ($setupChecks.sonarr) {
Write-ColorOutput -ForegroundColor green -MessageData "Sonarr"
} else {
Write-ColorOutput -ForegroundColor red -MessageData "Sonarr"
}
Write-ColorOutput -nonewline -MessageData " 2) "
if ($setupChecks.sonarr4k) {
Write-ColorOutput -ForegroundColor green -MessageData "Sonarr 4K"
} else {
Write-ColorOutput -ForegroundColor red -MessageData "Sonarr 4K"
}
Write-ColorOutput -nonewline -MessageData " 3) "; Write-ColorOutput -ForegroundColor gray -MessageData "Back to Main Menu"
Write-Information ""
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Selection: "
$ans = Read-Host
try {
$ans = [int]$ans
} catch {
[int]$ans = 0
}
if (-Not(($ans -ge 1) -And ($ans -le 3))) {
Write-Information ""
Write-ColorOutput -ForegroundColor red -MessageData "You did not specify a valid option!"
$valid = $false
} elseif (($ans -ge 1) -And ($ans -le 2)) {
$valid = $true
setupArr ($ans + 10)
} elseif ($ans -eq 3) {
$valid = $true
mainMenu
}
} while (-Not($valid))
}
# Print the Radarr menu and get response
function radarrMenu() {
do {
Write-Information ""
Write-Information "+---------------------------------------+"
Write-Information "| Radarr Setup Menu |"
Write-Information "+---------------------------------------+"
Write-ColorOutput -ForegroundColor gray -MessageData "Please choose which version of Radarr you"
Write-ColorOutput -ForegroundColor gray -MessageData "would like to configure for MediaButler: "
Write-Information ""
Write-ColorOutput -nonewline -MessageData " 1) "
if ($setupChecks.radarr) {
Write-ColorOutput -ForegroundColor green -MessageData "Radarr"
} else {
Write-ColorOutput -ForegroundColor red -MessageData "Radarr"
}
Write-ColorOutput -nonewline -MessageData " 2) "
if ($setupChecks.radarr4k) {
Write-ColorOutput -ForegroundColor green -MessageData "Radarr 4K"
} else {
Write-ColorOutput -ForegroundColor red -MessageData "Radarr 4K"
}
Write-ColorOutput -nonewline -MessageData " 3) "
if ($setupChecks.radarr3d) {
Write-ColorOutput -ForegroundColor green -MessageData "Radarr 3D"
} else {
Write-ColorOutput -ForegroundColor red -MessageData "Radarr 3D"
}
Write-ColorOutput -nonewline -MessageData " 4) "; Write-ColorOutput -ForegroundColor gray -MessageData "Back to Main Menu"
Write-Information ""
Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "Selection: "
$ans = Read-Host
try {
$ans = [int]$ans
} catch {
[int]$ans = 0
}
if (-Not(($ans -ge 1) -And ($ans -le 4))) {
Write-ColorOutput -ForegroundColor red -MessageData "You did not specify a valid option!"
$valid = $false
} elseif (($ans -ge 1) -And ($ans -le 3)) {
$valid = $true
setupArr ($ans + 20)
} elseif ($ans -eq 4) {
$valid = $true
mainMenu
}
} while (-Not($valid))
}
# Function to get the Tautulli information, test it and send it to the MediaButler server
function setupTautulli() {
if ($setupChecks.tautulli -eq $true) {
do {
Write-ColorOutput -ForegroundColor red -MessageData "Tautulli appears to be setup already!"
Write-ColorOutput -ForegroundColor yellow -MessageData "Do you wish to continue?"
Write-ColorOutput -ForegroundColor green -nonewline -MessageData "[Y]"; Write-ColorOutput -ForegroundColor gray -nonewline -MessageData "es or "; Write-ColorOutput -ForegroundColor red -nonewline -MessageData "[N]"; Write-ColorOutput -ForegroundColor gray -MessageData "o";
$answ = Read-Host
if (($answ -notlike "y") -And ($answ -notlike "yes") -And ($answ -notlike "n") -And ($answ -notlike "no")) {
Write-ColorOutput -ForegroundColor red -MessageData "Please specify yes, y, no, or n."
$valid = $false
} elseif (($answ -like "y") -Or ($answ -like "yes")) {
$valid = $true
} elseif (($answ -like "n") -Or ($answ -like "no")) {
$valid = $true
mainMenu
}
} while (-Not($valid))
}
# Tautulli URL
do {
Write-Information ""
Write-ColorOutput -ForegroundColor gray -MessageData "Please enter your Tautulli URL (IE: http://127.0.0.1:8181/tautulli/):"
$tauURL = Read-Host
$lastChar = $tauURL.SubString($tauURL.Length - 1)
if ($lastChar -ne "/") {
$tauURL = "$tauURL/"
}
#Write-Information ""
#Write-ColorOutput -ForegroundColor gray -MessageData "Checking that the provided Tautulli URL is valid..."
#try {
# $formattedURL = [System.String]::Concat(($tauURL), 'auth/login')
# $response = Invoke-WebRequest -Uri $formattedURL -TimeoutSec 10 -UseBasicParsing
# [String]$title = $response -split "`n" | Select-String -Pattern '<title>'
#} catch {
# Write-Debug $_.Exception.Message
#}
#if ($title -like "*Tautulli*") {
# Write-ColorOutput -ForegroundColor green -MessageData "Success!"
# $valid = $true
#} else {
# Write-ColorOutput -ForegroundColor red -MessageData "There was an error while attempting to validate the provided URL!"
# $valid = $false
#}
#} while (-Not($valid))
# API Key
#do {
Write-Information ""
Write-ColorOutput -ForegroundColor gray -MessageData "Please enter your Tautulli API key"
$tauAPI = Read-Host -AsSecureString
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList "apiKey", $tauAPI
$tauAPI = $credentials.GetNetworkCredential().Password
Write-Information ""
Write-ColorOutput -ForegroundColor gray -MessageData "Testing that the provided Tautulli API Key is valid..."
try {