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!



2012/06/01

Windows Shortcuts With Relative Path

Problem

You cannot use the relative path to executable in shortcuts (under Windows OS).

Solution

It's doable with a little help of the "start" command in Windows shell.

Example

We have the "MobileFolder" and we want to have shortcuts in it to some apps located in different sub-folders, which will works even if the "MobileFolder" will be moved to different location. Or the "MobileFolder" is on USB disk, which on different PCs can have assigned different drive letter.






Set...
Full Target: %WinDir%\System32\cmd.exe /c start "App1" "Apps\App1\procexp.exe"
Start in: <empty>
Run: Minimized (for not occurring the command line window even on a fraction of a second)

You can "Change Icon..." of the shortcut to your desirable icon, but you have to choose only from system's built-in icons: %SystemRoot%\system32\SHELL32.dll


2012/05/23

Build a cheap AirPlay alternative

What we'll need... is "shairport + OpenWrt"
  • HW:
    • an OpenWrt capable router with USB port
    • an USB sound card
  • SW:
    • an OpenWrt firmware with enabled IPv6, ALSA and USB sound support in the kernel (+ functional "hostname" command)
    • the Shairport binaries (C package is recommended, because .pl version has more bugs - for now)
    • working Avahi-daemon with .service config file for shairport
  • Other:
    • at least basic Linux skills (if you'll find all necessary packages)
        • or
    • some Linux (OpenWrt) compiling skills

Ease way

For Asus WL-500G Premium (v1/2)

  • Just install this OpenWrt brcm47xx firmware (5,69 MB, with 2.6 kernel, squashfs), which includes:
    • enabled IPv6, USB storage & audio support
    • shairport 0.05 (with configured avahi-daemon, enabled auto-start through /etc/rc.local, and enabled also on WAN through /etc/firewall.user)
    • alsamixer, alsautils, fdisk, e2fsprogs, nano, mc
    • extroot ready

For Netgear WNDR3700 / WNDR3800

 OR
  • If you have all necessary kernel modules etc., try install this shairport package (opkg install <path to package>)
  • And create the config file for avahi-daemon (/etc/avahi/services/shairport.service) with this content:
<?xml version="1.0" standalone='no'?><!--*-nxml-*-->
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">Airplay Server on %h</name>
<service>
<type>_raop._tcp</type>
<port>5000</port>
<txt-record>txtvers=1</txt-record>
<txt-record>vn=3</txt-record>
<txt-record>pw=false</txt-record>
<txt-record>sr=44100</txt-record>
<txt-record>ss=16</txt-record>
<txt-record>ch=2</txt-record>
<txt-record>cn=0,1</txt-record>
<txt-record>et=0,1</txt-record>
<txt-record>ek=1</txt-record>
<txt-record>sv=false</txt-record>
<txt-record>sm=false</txt-record>
<txt-record>tp=TCP,UDP</txt-record>
</service>
</service-group> 

  • Reboot
  • Start Shairport (shairport -a "WiFi Speakers" -o 5000 -d)

Hard way

Build your own OpenWrt firmware with enabled IPv6, ALSA and USB sound support in the kernel (+ functional "hostname" command)
.. and Shairport binaries, with added feed (src-git jlars git://github.com/jlars/packages.git;master) to feed.conf:
...
..openwrt/trunk# make dirclean
..openwrt/trunk# svn update
..openwrt/trunk# ./scripts/feeds update -a
..openwrt/trunk# ./scripts/feeds install shairport
..openwrt/trunk# ./scripts/feeds install -a
..openwrt/trunk# make menuconfig
..openwrt/trunk# make
...

Final Notes & Tips

Flashing router's firmware is always at your own risk. You will probably loose the warranty for your router. Don't do it, if you don't know what you do.


For shairport automatic startup just add the shairport -a Speakers -o 5000 -b 50 -d to /etc/rc.local file.

If you want to provide the AirPlay functionality over router's WAN port, you'll need open communication on firewall. You can do this by adding this to /etc/firewall.user:

# Allow avahi-daemon ond WAN
# for both IPv4 and IPv6
config rule
        option name             Allow-Avahi
        option src              wan
        option proto            udp
        option dest_port        5353
        option target           ACCEPT
        option family           any


# Allow Shairport ond WAN
# for both IPv4 and IPv6
config rule
        option name             Allow-Shairport
        option src              wan
        option proto            tcpudp
        option dest_port        5000
        option target           ACCEPT
        option family           any
For controlling the USB sound card volume you can use alsamixer command line tool. Settings will not survive reboot, therefore use alsactl store / alsactl restore commands for saving / restoring settings (after reboot). For using it you'll need probably create /var/lib/alsa/asound.state file first. Again, use rc.local for restoring the settings after startup.


Cheapest and smallest OpenWrt capable router with USB port, is TP-Link TL-WR703N (about $20) at the moment. You'll need go the "hard way" with it anyway and build your own firmware. It has only of 4MB flash memory size. You'll need use the USB hub + a flash disk + extroot probably.
Cheapest USB sound card you can find on eBay. Even bellow $2.

Enjoy

2011/10/25

Service Pack installation failed with error code 0x800f0818.

If on (fresh installed) Windows Server 2008 R2 you are not able to install SP1 with error code 0x800f0818 in System's Event Viewer (Event ID 8), check this log file: "C:\Windows\Logs\CBS\CheckSUR.log" (after installing KB947821).

Then, maybe you'll want try following steps:

1. Download this x64 KB from Microsoft (Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 and Windows Server 2008 R2):

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=26289

2. Expand the msu package.

Example:
expand C:\sourceDir\Windows6.1-KB2518867-x64.msu /f:* c:\tempDir

3. Expand the cab file.

Example:
expand C:\tempDir\Windows6.1-KB2518867-x64.cab /f:* c:\targetDir

4. Change Ownership of "C:\Windows\servicing\packages" folder to your account and give for your account full control privilege (you will need it for last step).

5. Replace this files***:
   C:\Windows\servicing\packages\Package_for_KB2518867_RTM~31bf3856ad364e35~amd64~~6.1.1.0.mum
   C:\Windows\servicing\packages\Package_for_KB2518867_RTM~31bf3856ad364e35~amd64~~6.1.1.0.cat

   with the same name files from "c:\targetDir"



*** files for step 5 may be different according to errors in CheckSUR.log file.

2011/01/26

Maintenance guys

Average "IT guy" is in fact "Maintenance guy". That's from my observation and experiences. Who don't want to be average, have the only way to success in that - neverending looking for improvements - be "still under construction". Unfortunatelly it can brings also never-ending dissatisfaction. With myself, or with things around me, eventually with both.
Hope it will not brings me the pills, like to my boss (and to my ex-boss)...
:o)

Another observed fact: main purpose of most IT jobs should be general automation. "On the end will be no IT jobs..." can someone say, but the opposite is the truth. It's only natural demand coming from widely spreading of IT technologies.