-
Notifications
You must be signed in to change notification settings - Fork 0
/
Check-PlexTuner.ps1
68 lines (57 loc) · 2.51 KB
/
Check-PlexTuner.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
param(
[string]$Token,
[ValidateSet("http", "https")]
[string]$Protocol = "http",
[string]$ServerIPorHost = "127.0.0.1",
[int]$ServerPort = 32400,
[int]$Timeout = 10,
[switch]$CheckForActivity,
[string]$PlexPath = "C:\Program Files (x86)\Plex\Plex Media Server\Plex Media Server.exe"
)
function Get-PlexActive{
$active = $false
### Check if antything is playing ###
$url = "{0}://{1}:{2}/status/sessions?X-Plex-Token={3}" -f $Protocol,$ServerIPorHost,$ServerPort,$token
$result = Invoke-RestMethod -uri $url
if($result.MediaContainer.Size -notlike "0"){ $active = $true }
### If nothing is playing, check if anything is recording ###
if(!$active){
$url = "{0}://{1}:{2}/media/subscriptions/scheduled?X-Plex-Token={3}" -f $Protocol,$ServerIPorHost,$ServerPort,$token
$result = Invoke-RestMethod -uri $url
$inprog = $result.MediaContainer.MediaGrabOperation | WHERE status -eq "inprogress" | ft -AutoSize
if($inprog){ $active = $true }
}
### Return the active state
return $active
}
### Get the Plex Update Service, PMS Process, and Tuner Service Process
$srvPlex = Get-Service "PlexUpdateService"
$prcPlex = Get-Process "Plex Media Server"
$prcTune = Get-Process "Plex Tuner Service" -ErrorAction SilentlyContinue
### If the Tuner service is not running, startthe process of restarting it
if(!($prcTune)){
Write-Host "Tuner is not running."
if($CheckForActivity){
$startTime = Get-Date
Write-Host " - Checking if media is playing..."
### Check every second for $Timeout seconds for Plex to be idle
while( Get-PlexActive -and ((get-date) - $startTime).TotalSeconds -lt $Timeout ){
Write-Host " - Waiting for Plex to be Idle..."
Start-Sleep -Seconds 1
}
}
Write-Host " - Ending Process Gracefully..."
$stopStart = Get-Date
$prcPlex | Stop-Process
while(($prcPlex | Get-Process -ErrorAction SilentlyContinue) -and ( ((Get-Date) - $stopStart).TotalSeconds -le $timeout )){ Start-Sleep -Milliseconds 100 }
$stopStart = Get-Date
if($prcPlex | Get-Process -ErrorAction SilentlyContinue){
Write-Host " - Graceful termination failed, force quitting Plex Process..."
$prcPlex | Get-Process | Stop-Process -Force
}
while(($prcPlex | Get-Process) -and ( ((Get-Date) - $stopStart).TotalSeconds -le $timeout )){ Start-Sleep -Milliseconds 100 }
Start-Process -FilePath $PlexPath
Start-Sleep -Seconds 1
}
else{ Write-Host "Tuner is running" }
Get-Process plex*