2017/09/28

Update script for Plex server service (PMS as a service) under Windows

It's strange Plex is not using "the service approach" natively on Windows (as 09/2017), but nothing is perfect (even our loved Plex) these days... So if someone wants to run Plex Media Server (PMS) as a Windows service, the good working way is to use the PmsService (as we can found on Plex forum or on Google). 
Few moments later the one probably realized "And what about updates? Do I have to do it manually...? Or, it should be possible to use some simple CMD/BAT script for that, somehow..."

EDIT:
Version 1.0 with auto-download and email notification is ready on GitHub: https://github.com/Mart11/PlexUpdate4Win

OLD post continue:
Well, here is what I'm using for it (change the paths for PMSEXE and UPDATEROOT according to your system environment):

@echo off

:: Update patch root folder
set PMSEXE=c:\\Program Files (x86)\\Plex\\Plex Media Server\\Plex Media Server.exe
set UPDATEROOT=c:\Users\admin\AppData\Local\Plex Media Server\Updates 
set LASTVER=
for /f "tokens=1 delims=-" %%a in ('dir "%UPDATEROOT%\" /B /D') do set LASTVER=%%a 
set LASTVERDIR=
for /f %%a in ('dir "%UPDATEROOT%\" /B /D') do set LASTVERDIR=%%a
for /f %%a in ('dir "%UPDATEROOT%\%LASTVERDIR%\packages\" /B') do set LASTVERSETUP=%%a 
set EXEVER=
for /F "tokens=2 delims==" %%a in ('wmic datafile where "name='%PMSEXE%'" get Version /value') do set EXEVER=%%a
echo Latest available installer: %LASTVER%
echo Installed version: %EXEVER%

:: If the last version is same as installed then end the script
if %LASTVER%==%EXEVER% goto :skip
set LASTVER1=
set EXEVER1=
set LASTVER2=
set EXEVER2=
set LASTVER3=
set EXEVER3=
set LASTVER4=
set EXEVER4=

:: Install new version if already installed version is not higher as latest available installer
for /f "tokens=1,2,3,4 delims=." %%a in ("%LASTVER%") do set LASTVER1=%%a & set LASTVER2=%%b & set LASTVER3=%%c & set LASTVER4=%%d
for /f "tokens=1,2,3,4 delims=." %%a in ("%EXEVER%") do set EXEVER1=%%a & set EXEVER2=%%b & set EXEVER3=%%c & set EXEVER4=%%d
if %LASTVER1% GTR %EXEVER1% (
goto :install
) ELSE (
if %LASTVER2% GTR %EXEVER2% (
goto :install
) ELSE (
if %LASTVER3% GTR %EXEVER3% (
goto :install
) ELSE (
if %LASTVER4% GTR %EXEVER4% (
goto :install
)
)
)
)
echo Installed version is newer that available installer.
echo Finished
goto :eof

:skip
echo Latest version is already installed. Skipping update.
echo Finished
goto :eof

:install
echo Installing newer version: %LASTVER% ...
:: First stop PMS, then run update
taskkill /F /IM "Plex Media Server.exe" /T
"%UPDATEROOT%\%LASTVERDIR%\packages\%LASTVERSETUP%" /install  /quiet
echo Finished

:eof
If you want to log output to the file, you can call the main script through some startup script, ie. as this one:

@echo off
set LOG="<Path to PmsServiceUpdate.log>"
set MAINSCRIPT="<Path to main CMD/BAT script>"

echo %date% %time% >> %LOG%
call %MAINSCRIPT% >> %LOG%
echo. >> %LOG% 
I'm runnig this second "PmsServiceUpdate-withLOG.cmd" which calls the first "PmsServiceUpdate.cmd" with the Windows system scheduler dayly in the morning. When a new version is installing, PMS process will be terminated and installer will cleanup any PMS/Plex processes. Then, PmsService (settings through systray icon) will restart the Plex Media Server after 300 seconds by default, when it detects PMS down. This 300 seconds should be more that enough for installation process - or you can adjust it if needed.


Enjoy!



No comments:

Post a Comment