Advertisement
upz

Update Plex (Ubuntu-64) from Windows - 1 click

upz
Dec 29th, 2017
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #----------------------------------------#
  2. # Update plex auto                       |
  3. # This Module is needed                  |
  4. # Install-Module -Name Posh-SSH -Verbose |
  5. #----------------------------------------#
  6.  
  7. [CmdLetBinding()]
  8. Param
  9. (
  10.     [Parameter(Mandatory=$true,Position=0, HelpMessage='Plex Ip')]
  11.     [string]$ServerIp,
  12.     [Parameter(Mandatory=$true,Position=0, HelpMessage='Ubuntu pw')]
  13.     [string]$pw,
  14.     [Parameter(Mandatory=$true,Position=0, HelpMessage='Ubuntu UserName')]
  15.     [string]$User    
  16. )
  17.  
  18. #----------------------#
  19. # Find Distro you need |
  20. #----------------------#
  21.  
  22. $plex = Invoke-RestMethod -Uri 'https://plex.tv/api/downloads/1.json?_=1514585869146'
  23.  
  24. foreach ($item in $plex.computer.Linux.releases)
  25. {
  26.     if ($item.label -match 'Ubuntu 64-bit')
  27.     {
  28.         $plex_ubuntu = $item
  29.         $plex_ubuntu | Add-Member -MemberType NoteProperty -Name 'Version' -Value $plex.computer.Linux.version
  30.     }    
  31. }
  32.  
  33.  
  34. #-----------------------------#
  35. # Download and install update |
  36. #-----------------------------#
  37.  
  38. $secpasswd = ConvertTo-SecureString $pw -AsPlainText -Force
  39. $mycreds = New-Object System.Management.Automation.PSCredential ($User, $secpasswd)
  40.  
  41. try
  42. {
  43.     # Create Session and Stream
  44.     $s = New-SSHSession -ComputerName $ServerIp -AcceptKey -Credential $mycreds -ErrorAction SilentlyContinue
  45.     $secpas = ConvertTo-SecureString $pw -AsPlainText -Force
  46.     $stream = $s.Session.CreateShellStream("PS-SSH", 200, 0, 0, 0, 1000)
  47.     $SSHusersName = Invoke-SSHCommandStream -Command 'whoami' -SSHSession $s
  48.     $Hostname = Invoke-SSHCommandStream -Command 'hostname' -SSHSession $s
  49.    
  50.     # Check plex Version, if up todate exit, otherwise update plex
  51.     $version = Invoke-SSHStreamExpectSecureAction -ShellStream $stream -Command "sudo dpkg -l plexmediaserver" -ExpectString "[sudo] password for $($SSHusersName):" -SecureAction $secpas
  52.     Start-Sleep 1
  53.     $v = $stream.read()
  54.     $v = $v.split("`r`n") | where {$_ -match 'plexmediaserve'}
  55.     $v = (([regex]::Replace($v,"\s{2,}"," ")).split(" ")[2]).trim()
  56.    
  57.     if ([regex]::Match($v,$plex_ubuntu.Version).success)
  58.     {
  59.         Write-Host -ForegroundColor Yellow 'Plex is up todate'        
  60.     }
  61.     else
  62.     {
  63.         $results = Invoke-SSHStreamExpectSecureAction -ShellStream $stream -Command "sudo wget $($plex_ubuntu.url); sudo dpkg -i $($plex_ubuntu.url.split('/')[-1])" -ExpectString "[sudo] password for $($SSHusersName):" -SecureAction $secpas
  64.         $count = 0
  65.         do
  66.         {
  67.             clear
  68.             $status = $stream.Read()
  69.             Write-Host -ForegroundColor Yellow $status
  70.             Start-Sleep -Milliseconds 500
  71.             $count++
  72.         } until ($status.Contains("$user@$($hostname):~$") -or $count -gt 60)
  73.         Write-Host -ForegroundColor Green "Plex is updatet to $($plex_ubuntu.Version)"        
  74.     }
  75.     $s.Disconnect()    
  76. }
  77. catch
  78. {
  79.     Write-Host -ForegroundColor Red 'Script failed, to lazy to write prober Exception'
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement