-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlexMediaServerUpdater.ps1
177 lines (152 loc) · 6.78 KB
/
PlexMediaServerUpdater.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
<#
.SYNOPSIS
Powershell script to update PLEX automatically
.DESCRIPTION
This powershell script scans the PLEX folder for new downloaded versions and installs them automatically.
This script normally doesn't require any parameter, because if PLEX is running, it can determine all that it needs to find the PLEX folder with the new installers and install them.
.PARAMETER userName
The name of the user account under which PLEX runs, it is important because the new downloaded installers are in app data folder of the user.
If omitted and PLEX is running, the script can determine it automatically.
.PARAMETER serviceName
The name of the windows service under which PLEX runs, it is important if PLEX run as a service, because before the installation the service has to be stopped and after restarted.
If omitted and PLEX is running, the script can determine it automatically.
.PARAMETER keepInstallerFile
Normally the script deletes the installer file after the installation to free space on the hard disk.
To not delete it and keep it uses this parameter.
.EXAMPLE
./PlexMediaServerUpdater.ps1
It scans the folder of the local instance of PLEX for a newer installer and install it
.EXAMPLE
./PlexMediaServerUpdater.ps1 -keepOldInstallers
It scans the folder of the local instance of PLEX for a newer installer and install it, but it won't delete the old installer files
.LINK
https://github.com/aquilax1/Plex-Media-Server-Updater
#>
param([String] $userName, [String] $serviceName, [Switch] $keepOldInstallers)
function getPlexService
{
#Get service from service name
if (![System.String]::IsNullOrEmpty($serviceName)) { return get-service $serviceName }
#Get service from Plex process, if it is running
$ser=$Null
$pro=$process
while ($ser -eq $Null -and $pro -ne $Null)
{
$ser=gwmi Win32_Service -Filter ("ProcessId="+$pro.ProcessId)
$pro=gwmi Win32_Process -Filter ("ProcessId="+$pro.ParentProcessId)
}
if ($ser -ne $Null) { return get-service $ser.Name}
}
function getPlexExecutablePath
{
#Get Plex install path from registry
$path = Get-ItemProperty "HKCU:\Software\Plex, Inc.\Plex Media Server" -Name "InstallFolder" -ErrorAction SilentlyContinue
if ($path -match "InstallFolder" ) { return $path.InstallFolder+"\Plex Media Server.exe" }
#Get Plex install path from Plex process, if Plex is running
if ($process -ne $Null) { return $process.ExecutablePath }
#Get Plex default install path
$programs=$env:ProgramFiles
if (Test-Path ($programs+" (x86)")) {$programs=$programs+" (x86)"}
$plex_path=$programs+"\Plex\Plex Media Server\Plex Media Server.exe"
if (Test-Path $plex_path) { return $plex_path }
#Nothing found, throw exception
throw [System.Exception] "Can't find Plex installation path"
}
function getPlexUserAccount
{
#Get user name from Plex process, if Plex is running
if ($process -ne $Null) { return $process.GetOwner().User }
#Get user name from service, if there is one
if ($service -ne $Null) { return $service.StartName }
#Nothing found, throw exception
throw [System.Exception] "Can't determine user account if plex is not running and no service name is specified"
}
function installPlex
{
try
{
Write-Host (get-date) "Starting Plex installer"
Start-Process $installer.FullName -ArgumentList "/install /quiet /norestart" -Wait
Write-Host (get-date) "Installation completed"
}
catch { Write-Host (get-date) "Error:" $_.Exception.Message }
}
try
{
$web=New-Object System.Net.WebClient
#Get Plex process, if it is running
$process=gwmi Win32_Process -Filter "Name='Plex Media Server.exe'"
#Write-Host (Get-Date) "Plex is running?" ($process -ne $Null)
#Get Plex service, if there is one
$service=getPlexService
#Write-Host (Get-Date) "Plex is running as a service?" ($service -ne $Null)
#Check for same user in case of plex running as a desktop application
if ($service -eq $Null -and $process -ne $Null -and $process.GetOwner.User -ne $Env:UserName) { throw [System.Exception] "For Plex running as a desktop application this updater script must be executed under the same user account of Plex"}
#Get Plex version from executable
$plex_version=[System.Version] [System.Diagnostics.FileVersionInfo]::GetVersionInfo((getPlexExecutablePath)).FileVersion
#Write-Host (Get-Date) "Plex current installed version is" $plex_version
#Get current release from Plex API
$current_release=(ConvertFrom-Json $web.DownloadString("https://plex.tv/pms/downloads/5.json")).computer.Windows
$current_version=[System.Version][regex]::match($current_release.version,"\d+\.\d+\.\d+\.\d+").Value
if ($current_version -le $plex_version)
{
Write-Host (Get-Date) "No new version available"
}
else
{
Write-Host (Get-Date) "Downloading new version" $current_version
$url=$current_release.releases[0].url
#write-host (get-date) $url
$path=$Env:temp+$url.substring($url.LastIndexOf("/"))
#write-host (get-date) $path
$web.DownloadFile($url,$path)
$installer=Get-ChildItem $path
if ($installer -ne $Null)
{
Write-Host (Get-Date) "Installing Plex version" ($installer_path.Version)
if ($service -ne $Null)
{
#Installing Plex when running as a service
if ($service.Status -eq "Running")
{
Write-Host (get-date) "Stopping Plex service"
$service.Stop()
$service.WaitForStatus("Stopped",[TimeSpan]::FromSeconds(10))
if ($service.Status -ne "Stopped") { throw [System.Exception] "Can't stop Plex service" }
}
installPlex
Write-Host (get-date) "Removing Plex from startup programs"
Remove-ItemProperty "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run\" -Name "Plex Media Server" -ErrorAction SilentlyContinue
Write-Host (get-date) "Starting Plex service"
$service.Start()
$service.WaitForStatus("Running",[TimeSpan]::FromSeconds(10))
if ($service.Status -ne "Running") { throw [System.Exception] "Can't start Plex service" }
}
else
{
#Installing Plex when running as a desktop application
if ($process -ne $Null)
{
Write-Host (get-date) "Stopping Plex"
$process=Get-Process "Plex Media Server"
$process.CloseMainWindow()
$process.WaitForExit(10000)
if (!$process.HasExited) { $process.Kill() }
$process.WaitForExit(5000)
if (!$process.HasExited) { throw [System.Exception] "Can't stop Plex" }
}
installPlex
Write-Host (get-date) "Starting Plex"
Start-Process getPlexExecutablePath
Start-Sleep -s 5
if ((Get-Process "Plex Media Server") -eq $Null) { throw [System.Exception] "Can't start Plex" }
}
}
if (-not $keepInstallerFile)
{
Write-Host (Get-Date) "Removing installer"
Remove-Item $installer -Force
}
}
}
catch { Write-Host (get-date) "Error:" $_.Exception.Message }