Jump to content

Share: My Plex Update Script

Hi Guys,

I was searching all over the web but couldn't find any simple update scripts for Plex.

So I made my own. I hope I can save someone the trouble of writing there own :)

The Script is based on PowerShell

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
$json            = Invoke-RestMethod -Uri "https://plex.tv/api/downloads/1.json"
$dl_url          = $json.computer.Windows.releases.url
$checksum        = $json.computer.Windows.releases.checksum
$Latest_Version  = $json.computer.Windows.version
$PlexServerIP    = "localhost:32400"
$XPlexToken      = "YOUR_PLEX_TOKEN"  #example: _aBcDEf_12g3hijkL4Mn
$Status_url      = "http://$PlexServerIP/status/sessions?X-Plex-Token=$XPlexToken"
$PlexServerPath  = "C:\Program Files (x86)\Plex\Plex Media Server\Plex Media Server.exe"
$PMSUpdate_Path  = "C:\Users\Administrator\AppData\Local\Plex Media Server\Updates"
$Server_Version  = (Get-ChildItem -Path $PMSUpdate_Path | sort LastWriteTime | select -last 1).Name
$Latest_Pkg_Path = "$PMSUpdate_Path\$Latest_Version\packages"

If($Latest_Version -ne $Server_Version)
{#Update Required
  New-Item -Path $Latest_Pkg_Path -ItemType Directory | Out-Null
  $dl_FilePath = "$Latest_Pkg_Path\PlexMediaServer-$Latest_Version-x86.exe"
  (New-Object System.Net.WebClient).DownloadFile($dl_url, $dl_FilePath)
  $dl_FileHash = (Get-FileHash -Algorithm SHA1 $dl_FilePath).Hash

  If($dl_FileHash -eq $checksum)
  { #Install Update
    do
    {
      $data           = Invoke-WebRequest -Uri $Status_url
      $isStreaming    = !($data -match '<MediaContainer size="0">')
      if($isStreaming){Start-Sleep -Seconds 10}
    }
    while($isStreaming)
    Start-Process -FilePath $dl_FilePath -ArgumentList "/install","/quiet" -Wait
    Start-Process -FilePath $PlexServerPath
  }             
}

What it does:  It checks for the newest version of Plex online, compairs it with the latest update-installer your server has. If the file on your server is older it will download the newest one, validates the checksum and Installs it.

 

Why do I need a X-Plex-Tolken?  -  It is not required, but if you enter your tolken the script can check if a user is still streaming and the script will wait with the install until the stream is closed.

(If you dont want to use this function you have to remove the "do-while".)

 

 

So have fun with it and if you have Ideas to improve it share them with us 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Why the hard coded IP address? I get that it is simpler but this could be made entirely portable except for the token. Otherwise, nice script.

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, djboy6480 said:

 


$PlexServerIP    = "192.168.178.2:32400"

$Status_url      = "http://$PlexServerIP/status/sessions?X-Plex-Token=$XPlexToken"

 

 

Nice script. But as @2FA suggested, i'd change this, into this

 

$GetIPAddress = Test-Connection -ComputerName (hostname) -Count 1  | Select -ExpandProperty IPV4Address
$PlexServerIP = $GetIPAddress.IPAddressToString

$Status_url      = "http://$PlexServerIP`:32400/status/sessions?X-Plex-Token=$XPlexToken"

0110d3d6f3dcf999cb4e81b92f39018c.png

Spoiler

Desktop: Ryzen9 5950X | ASUS ROG Crosshair VIII Hero (Wifi) | EVGA RTX 3080Ti FTW3 | 32GB (2x16GB) Corsair Dominator Platinum RGB Pro 3600Mhz | EKWB EK-AIO 360D-RGB | EKWB EK-Vardar RGB Fans | 1TB Samsung 980 Pro, 4TB Samsung 980 Pro | Corsair 5000D Airflow | Corsair HX850 Platinum PSU | Asus ROG 42" OLED PG42UQ + LG 32" 32GK850G Monitor | Roccat Vulcan TKL Pro Keyboard | Logitech G Pro X Superlight  | MicroLab Solo 7C Speakers | Audio-Technica ATH-M50xBT2 LE Headphones | TC-Helicon GoXLR | Audio-Technica AT2035 | LTT Desk Mat | XBOX-X Controller | Windows 11 Pro

 

Spoiler

Server: Fractal Design Define R6 | Ryzen 3950x | ASRock X570 Taichi | EVGA GTX1070 FTW | 64GB (4x16GB) Corsair Vengeance LPX 3000Mhz | Corsair RM850v2 PSU | Fractal S36 Triple AIO | 12 x 8TB HGST Ultrastar He10 (WD Whitelabel) | 500GB Aorus Gen4 NVMe | 2 x 2TB Samsung 970 Evo Plus NVMe | LSI 9211-8i HBA

 

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Jarsky said:

 


$GetIPAddress = Test-Connection -ComputerName (hostname) -Count 1  | Select -ExpandProperty IPV4Address
$PlexServerIP = $GetIPAddress.IPAddressToString

$Status_url      = "http://$PlexServerIP`:32400/status/sessions?X-Plex-Token=$XPlexToken"

 

Thanks for your feedback :)

 

Test-Connection is not really needed because the Script has to run localy on the server. I could have used localhost.

Why i do no IP-Parsing? Well because I wanted the script to be as light as possible.

If it is once setup correctly you will never touch it again so Parsing is imho just wasted cpu cycles. 😁

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×